@Autowired and @Service working with the controller, but not from another package

I need help understanding the concepts of @Autowired and @Service . I have a DAO defined using @Service and a controller with @Autowired and everything seems fine, however I use the same @Autowired in a different class, then it does not work.

Example:

Service

 @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"); } } 

controller

 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"; } } 

Above everything works fine. However, if I want to use MyService in POJO, then it just does not work. Example:

 package com.springtest.mywork.pojos; public class MyPojo { @Autowired MyService myService; public void testFromPojo () { myService.someDataAccessMethod(); //myService is still null } } 

Spring Configuration:

 <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> 
+6
source share
5 answers

This is because your POJO class is not managed by the spring container.

@Autowire annotation will only work with objects managed by spring (i.e. created by the spring container).

In your case, the service and controller object is managed by spring, but your POJO class is not managed by spring, so @Autowire does not create the behavior you expect.

Another problem that I noticed is that you are using the @Service annotation in the DAO layer when spring has the @Repository annotation specially created for this purpose.

It is also not recommended to allow spring to manage POJO classes, as these will usually be data storage elements that must be created outside the container.

Can you tell us what the purpose of the POJO class is and why it uses the service instance?

+12
source

When using classpath scanning, you need to contact Spring, which classes to manage. This is done using the @Service annotation and its relationships ( @Controller , @Repository , etc.).

If you decide not to comment on your bean, you must explicitly declare it in your configuration, as with dataSource and jdbcTemplate .

Annotating your classes means that only those classes in the package are managed by Spring; which allows you to scan a package without having to manage all classes in that package.

+5
source

Include this in applicationContext.xml

 <context:annotation-config /> 
+2
source

You can implement this to use spring -managed beans in your POJO class.

http://www.javacodegeeks.com/2015/03/using-spring-managed-bean-in-non-managed-object.html

0
source

check scanning of the context component in the configuration file

 <context:component-scan base-package="<your_package>" /> 
-1
source

Source: https://habr.com/ru/post/927583/


All Articles