【发布时间】:2014-05-27 15:33:55
【问题描述】:
我是 Spring 新手,我对注入有一些疑问和问题(可能是所选的架构)
我有两个项目(桌面应用程序)和一个类似于 API 的应用程序(新的和使用 Spring 制作的)
第一个应用程序使用 API 的方法(服务...)
从桌面应用程序调用这段代码
ServiceManager serviceManager = ServiceManager.getInstance(dbConn.getConnection());
FirstService firstService = (FirstService)serviceManager.getService(Table.FIRST);
System.out.println("Count: " + firstService.count());
在 API 应用程序中,有两个不同的模块,Service 和 DAO 模块。
在服务模块中:
public class FirstServiceImpl extends GenericService implements FirstService {
private final static String TABLENAME = "FIRST";
@Autowired
FirstDAO firstDAO;
public FirstServiceImpl(Connection con) {
super(con, TABLENAME);
}
public int count() throws SQLException {
firstDAO.init(connection);
return firstDAO.count();
}}
public class ServiceManager {
private Connection con;
private static ServiceManager instance;
public ServiceManager(Connection con) {
super();
this.con = con;
}
public static ServiceManager getInstance(Connection connection) {
if (instance == null) {
instance = new ServiceManager(connection);
}
return instance;
}
public GenericService getService(Table t) throws SQLException {
switch (t) {
case FIRST:
return new FirstServiceImpl(this.con);
case SECOND:
//return new SecondDAO(this.con);
default:
throw new SQLException("Trying to link to an unexistant service.");
}
}
在DAO模块中
@Configuration
public class DAOConfig {
@Bean
public FirstDAO firstDAO() {
System.out.println("Scans!");
return new FirstDAOImpl();
}
}
和不相关的FirstDAOImpl。
我的应用程序上下文.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<context:component-scan base-package="com.ddd.iker.dao" />
</beans>
DAOConfig 在 com.ddd.iker.dao
我的问题是当我尝试执行 firstDAO.init() 时,FirstServiceImpl 中总是出现空指针异常
我尝试在ServiceManager的.getInstance方法中使用这段代码。
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:application-context.xml");
ctx.refresh();
@Bean 正在扫描,但我得到了同样的异常。
如果我在 FirstServiceImpl 的构造函数中设置这段代码,它可以工作(没有 Autowired),但我认为这不是最好的方法,因为我想使用注释。
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:application-context.xml");
ctx.refresh();
firstDAO = ctx.getBean("firstDAO", FirstDAO.class);
ctx.close();
如何在没有上述代码的情况下使用 @Autowired 注释获得 firstDAO?我应该如何管理应用程序上下文?我真的很感谢任何批评家。
【问题讨论】:
-
使用 Spring 时,bean 生命周期由 Spring 的应用程序上下文管理。因此,每当您致电
new SomeBean时,您都做错了。这样的 bean 将在 Spring 的影响之外创建 -> 即与 Spring 没有关系,没有自动装配,没有 AOP,... -
你能告诉我代码有什么问题吗,因为我不明白你的意思。
-
new ServiceManager- 不正确;new FirstServiceImpl- 不正确。让 Spring 创建这些 bean ......然后自动装配将起作用。只需保持对GenericXmlApplicationContext的静态引用并使用getBean。
标签: java spring dependency-injection