@Autowired和@Service在控制器上工作,但不是从不同的包中工作 [英] @Autowired and @Service working from controller but not from a different package
问题描述
我需要帮助理解 @Autowired
和 @Service
背后的概念。我有一个用 @Service
定义的DAO和带有 @Autowired
的控制器,一切似乎都很好,但是,我使用相同的 @Autowired
在另一个类中,然后它不起作用。
I need help understanding the concept behind @Autowired
and @Service
. I have a DAO defined with @Service
and controller with @Autowired
and everything seems fine, however, I use the same @Autowired
in a different class then it does not work.
示例:
服务
@Service
public class MyService {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource (DataSource myDataSource) {
this.jdbcTemplate = new JdbcTemplate(myDataSource);
}
public void testUpdate(){
jdbcTemplate.update("some query");
}
}
控制器
package com.springtest.mywork.controller;
@Controller
@RequestMapping(value = "/test.html")
public class MyController
{
@Autowired
MyService myService;
@RequestMapping(method = RequestMethod.GET)
public String test(Model model)
{
systemsService.testUpdate();
return "view/test";
}
}
以上一切正常。但是,如果我想在POJO中使用 MyService
,那么它就不起作用了。示例:
The above all works fine. However, If I want to use MyService
in a POJO then it just doesn't work. Example:
package com.springtest.mywork.pojos;
public class MyPojo {
@Autowired
MyService myService;
public void testFromPojo () {
myService.someDataAccessMethod(); //myService is still null
}
}
Spring配置:
<beans>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="com.springtest.mywork" />
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mydb" />
<property name="username" value="hello" />
<property name="password" value="what" />
</bean>
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
</beans>
推荐答案
这是因为您的POJO课程不受春季管理容器。
It is because your POJO class is not managed by spring container.
@Autowire
注释仅适用于由spring管理的对象(即由spring容器创建的对象) )。
@Autowire
annotation will work only those objects which are managed by spring (ie created by the spring container).
在你的情况下,服务和控制器对象由spring管理,但你的POJO类不是由spring管理的,这就是为什么 @Autowire
没有产生你期望的行为。
In your case the service and controller object are managed by spring, but your POJO class is not managed by spring, that is why the @Autowire
is not producing the behavior expected by you.
我注意到的另一个问题是,你使用的是 @当spring具有专门为此目的创建的
注释。 @Repository
注释时,DAO层中的服务
Another problem I noticed is, you are using the @Service
annotation in the DAO layer when spring has the @Repository
annotation specifically created for this purpose.
同样不允许spring管理POJO类,因为通常它是必须在容器外创建的数据存储元素。
Also it is not desirable to allow spring to manage the POJO classes since normally it will be data storage elements which has to be created outside the container.
可以你告诉我们POJO类的目的是什么以及为什么服务
in在它中使用了立场?
Can you tell us what is the purpose of the POJO class and why the service
instance is used in it?
这篇关于@Autowired和@Service在控制器上工作,但不是从不同的包中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!