gpt4 book ai didi

java - 从接口(interface)继承的spring注入(inject)bean

转载 作者:行者123 更新时间:2023-11-29 04:29:04 24 4
gpt4 key购买 nike

我有一些带有@Bean 注释的配置并且它可以工作。但是,如果我将 bean 更改为从接口(interface)继承,它就不起作用了。我需要特殊注释或类似的东西吗?

没有导入的配置类片段

@Configuration
@ComponentScan("io.github.aaa4")
//@ComponentScan({ "io.github.aaa4.dao", "io.github.aaa4.entity","io.github.aaa4.Servlets"})
@EnableTransactionManagement
public class AppConfig {

@Bean
public ComboPooledDataSource myDataSource() throws PropertyVetoException {
ComboPooledDataSource ds = new ComboPooledDataSource();

ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setJdbcUrl("jdbc:mysql://localhost:3306/emp?useSSL=false");
ds.setUser("emp");
ds.setPassword("emp");
ds.setMinPoolSize(2);
ds.setMaxPoolSize(20);
ds.setMaxIdleTime(30000);

return ds;
}

@Bean
public LocalSessionFactoryBean mySessionFactory() throws PropertyVetoException {
LocalSessionFactoryBean sf = new LocalSessionFactoryBean();
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
hibernateProperties.setProperty("hibernate.show_sql", "true");

sf.setDataSource(myDataSource());
sf.setPackagesToScan("io.github");
sf.setHibernateProperties(hibernateProperties);

return sf;
}

@Bean
@Autowired
public HibernateTransactionManager myTransactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager htm = new HibernateTransactionManager();
htm.setSessionFactory(sessionFactory);

return htm;
}


@Bean
public UserDao userDaoImpl() {
return new UserDaoImpl();
}

@Bean
public DBService dBServiceImpl() {
return new DBServiceImpl();
}

}

serviceImpl 片段

@Service
public class DBServiceImpl implements DBService {

//inject user DAO
@Autowired
private UserDao userDao;

@Transactional
public void addUser(MyUser user) {
userDao.createUser(user);
}

@Transactional
public List<MyUser> getUserList() {
return userDao.getusers();
}

@Transactional
public void deleteUser(int theId) {
userDao.deleteUser(theId);
}


}

主类

public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);

DBServiceImpl service = ctx.getBean(DBServiceImpl.class);

MyUser user = new MyUser("alice", "and her passwd");
System.out.println("this is the user:" + user);
service.addUser(user);
service.addUser(user);
service.addUser(user);
service.addUser(user);
System.out.println("User added!");
service.deleteUser(2);
System.out.println("user number id = 2 deleted");
List<MyUser> userList = service.getUserList();
for (MyUser users : userList)
System.out.println(users);
ctx.close();
}

}

有问题的堆栈跟踪

  INFO: Using DataSource [com.mchange.v2.c3p0.ComboPooledDataSource[ identityToken -> 1hge1459o1fr6qmlst9469|42e99e4a, dataSourceName -> 1hge1459o1fr6qmlst9469|42e99e4a ]] of Hibernate SessionFactory for HibernateTransactionManager

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'io.github.aaa4.services.DBServiceImpl' available

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)

at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at io.github.aaa4.Main.main(Main.java:30)

在配置类上方使用注解@EnableAspectJAutoProxy(proxyTargetClass = true)

WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:223)
at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:702)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:527)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at io.github.aaa4.Main.main(Main.java:14)
Caused by: java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.<clinit>(ReflectiveAspectJAdvisorFactory.java:77)
at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.initBeanFactory(AnnotationAwareAspectJAutoProxyCreator.java:78)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.setBeanFactory(AbstractAdvisorAutoProxyCreator.java:60)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1647)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1615)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
... 10 more
Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.annotation.Around
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 16 more

最佳答案

尝试使用接口(interface)而不是实现

DBService service = ctx.getBean(DBService.class);

您的服务方法上有 @Transactional 注释。所有 @Transactional 注释都应该在启动时扫描,目标方法应该成为事务性的。要添加事务行为,Spring 会为您的类创建一个代理。这就是您无法获取 bean 的原因。

关于java - 从接口(interface)继承的spring注入(inject)bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44613630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com