Spring 4 - Auto Component scanning example
Technologies used: JDK 1.8.0_121 | Spring 4.3.5.RELEASE | Maven 3.9.9 | Eclipse Mars.2 (4.5.2)
Spring provides four stereotype annotations:
@Component
, @Controller
, @Service
and @Repository
for spring-managed components.
The
@Controller
, @Service
and @Repository
annotations are specializations of the @Component
annotation for more specific purpose in presentation, service and data access layer respectively.
In Spring framework, we can enable the auto component scanning by using
- The
<context:component-scan/>
element in XML based configuration - The
@ComponentScan
annotation in java based configuration - The
AnnotationConfigApplicationContext#scan()
method
Create Spring Components
Create an
EmployeeService
bean under com.boraji.tutorial.spring.service
package as follows.
EmployeeService.java
package com.boraji.tutorial.spring.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.boraji.tutorial.spring.dao.EmployeeDao;
@Service
public class EmployeeService {
@Autowired
private EmployeeDao employeeDao;
public void doSomething(){
System.out.println("Inside EmployeeService's doSomething() method.");
employeeDao.doSomething();
}
}
Create an
EmployeeDao
bean under com.boraji.tutorial.spring.dao
package as follows.
EmployeeDao.java
package com.boraji.tutorial.spring.dao;
import org.springframework.stereotype.Repository;
@Repository
public class EmployeeDao {
public void doSomething() {
System.out.println("Inside EmployeeDao's doSomething() method.");
}
}
Enable component scanning using the <context:component-scan/> element
To enable auto component scanning in XML based configuration, use the
<context:component-scan base-package=""/>
element as follows.
Beans.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan
base-package="com.boraji.tutorial.spring.service,
com.boraji.tutorial.spring.dao" />
</beans>
Run application.
MainApp.java
package com.boraji.tutorial.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.boraji.tutorial.spring.service.EmployeeService;
/**
* @author imssbora
*/
public class MainApp {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext context1 = new ClassPathXmlApplicationContext("Beans.xml");
EmployeeService service1 = context1.getBean(EmployeeService.class);
service1.doSomething();
}
}
Output
Inside EmployeeService's doSomething() method.
Inside EmployeeDao's doSomething() method.
Comments
Post a Comment