Spring中有三种定义Bean的方式:
1. *.xml//
2. @Configuration/@Bean/@Import
3. @Component/@Controller/@Service/@Repository
之前的实例中,已经对前两种方式进行了记录,本章节记录@Component/@Controller/@Service/@Repository的方。
0.程序目录
project
\---src
\---main
\---java
| \---pers
| \---hanchao
| \---component
| \---App.java
| \---DemoComponent.java
| \---DemoController.java
| \---DemoService.java
| \---DemoRepository.java
\---resources
\---spring-config
\---spring-component.xml
1.App.java
package pers.hanchao.component;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* <p>通过@Component、@Controller、@Service、@Repository注解进行bean的定义,通过@Autowired进行bean的自动装配</p>
* @author hanchao 2018/1/7 19:51
**/
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config/spring-component.xml");
//@Component
DemoComponent demoComponent = (DemoComponent) context.getBean("demoComponent");
demoComponent.showMessage();
//@Controller、@Service、@Repository+@Autowired构成的三层注解
DemoController demoController = (DemoController) context.getBean("demoController");
demoController.showMessage();
}
}
2.spring-component.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.xsd">
<!--开启注解-->
<context:annotation-config />
<!--开启@Componet扫描,@Controller、@Service、@Repository都属于@Component注解-->
<context:component-scan base-package="pers.hanchao.*"/>
</beans>
3.DemoComponent.java
package pers.hanchao.component;
import org.springframework.stereotype.Component;
@Component
public class DemoComponent {
public void showMessage(){
System.out.println("@Component is success!");
}
}
4.DemoController.java
package pers.hanchao.component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class DemoController {
@Autowired
private DemoService demoService;
public void showMessage(){
this.demoService.showMessage();
System.out.println("@Controller is success!");
}
}
5.DemoService.java
package pers.hanchao.component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
@Autowired
private DemoRepository demoRepository;
public void showMessage() {
this.demoRepository.showMessage();
System.out.println("@Service is success!");
}
}
6.DemoRepository.java
package pers.hanchao.component;
import org.springframework.stereotype.Repository;
@Repository
public class DemoRepository {
public void showMessage() {
System.out.println("@Repository is success!");
}
}
7.result
@Component is success!
@Repository is success!
@Service is success!
@Controller is success!