Java程序员常用的@Component、@Repository、@Controller、@Service系列【案例demo3】
很多程序员通过在类上使用@Repository、@Component、@Service 和 @Constroller 注解,Spring会自动创建相应的 BeanDefinition 对象,并注册到 ApplicationContext 中。这些类就成了 Spring受管组件。这三个注解除了作用于不同软件层次的类,其使用方式与@Repository 是完全相同的。
处理类:org.springframework.context.annotation.ScannedGenericBeanDefinition
[if !supportLists]· [endif]项目包结构
F:.
├─java
│ └─com
│ └─example
│ └─demo3
│ │ Demo3Application.java
│ │
│ ├─controll
│ │ StuController.java
│ │
│ ├─dao
│ │ StuDao.java
│ │ StuDaoImp.java
│ │
│ ├─entity
│ │ Stu.java
│ │
│ └─server
│ StuService.java
│ StuServiceImp.java
│
└─resources
application.properties
project.text
[if !supportLists]· [endif]控制器角色StuController
package com.example.demo3.controll;
import org.springframework.stereotype.Controller;
@Controller
public class StuController {
}
[if !supportLists]· [endif]数据角色StuDao、StuDaoImp
package com.example.demo3.dao;
public interface StuDao {
}
package com.example.demo3.dao;
import org.springframework.stereotype.Repository;
@Repository
public class StuDaoImp implements StuDao{
}
[if !supportLists]· [endif]服务角色StuService、StuServiceImp
package com.example.demo3.server;
public interface StuService {
}
package com.example.demo3.server;
import org.springframework.stereotype.Service;
@Service
public class StuServiceImp implements StuService {
}
[if !supportLists]· [endif]其它组件角色Stu
package com.example.demo3.entity;
import org.springframework.stereotype.Component;
@Component
public class Stu {
String name;
public Stu(String name) {
this.name = name;
}
public Stu() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Stu{" +
"name='" + name + '\'' +
'}';
}
}
[if !supportLists]· [endif]Demo3Application(启动程序)
package com.example.demo3;
import com.example.demo3.controll.StuController;
import com.example.demo3.dao.StuDao;
import com.example.demo3.entity.Stu;
import com.example.demo3.server.StuService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Demo3Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Demo3Application.class, args);
//@Component注册的组件,名称默认都是类名的首字母小写
//纯属于注解方式注册组件
//之所以能力扫描到这些包,因为注解@AutoConfigurationPackage的作用(但必须满足所有组件都在启动类所在包的平级或子集)
StuController stuController = context.getBean("stuController", StuController.class);
Stu stu = context.getBean("stu", Stu.class);
StuDao stuDaoImp = context.getBean("stuDaoImp", StuDao.class);
StuService stuServiceImp = context.getBean("stuServiceImp", StuService.class);
//打印都有地址
System.out.println(stuController);
System.out.println(stu);
System.out.println(stuDaoImp);
System.out.println(stuServiceImp);
context.close();
}
}
AnnotationConfigApplicationContext与ConfigurableApplicationContext的关系
总结:
1.上下文基本架构关系
2.ConfigurableApplicationContext:
3.ClassPathXmlApplicationContext:
4.AnnotationConfigApplicationContext: