IoC 容器
- 配置元数据
基于 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="org.example.Student">
</bean>
</beans>
实体类代码
public class Student {
private String name,address,qq,email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", qq='" + qq + '\'' +
", email='" + email + '\'' +
'}';
}
}
启动类代码
public class App
{
public static void main( String[] args )
{
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("appliaction.xml");
Student student= (Student) applicationContext.getBean("student");
student.setAddress("西安碑林区");
student.setName("三毛毳毳");
student.setEmail("1172469573.qq.com");
student.setQq("1172469573");
System.out.println(student);
}
}
- 实例化容器 可以按功能分类定义多个配置文件
- 加载多个xml文件
public class App
{
public static void main( String[] args )
{
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("appliaction.xml","controller.xml","dao.xml","services.xml");
Student student= (Student) applicationContext.getBean("student");
student.setAddress("西安碑林区");
student.setName("三毛毳毳");
student.setEmail("1172469573.qq.com");
student.setQq("1172469573");
System.out.println(student);
StudentDao studentDao= (StudentDao) applicationContext.getBean("studentDao");
studentDao.setFunction("数据库");
studentDao.setName("DAO");
System.out.println(studentDao);
StudentService studentService= (StudentService) applicationContext.getBean("studentService");
studentService.setFunction("服务");
studentService.setName("SERVICE");
System.out.println(studentService);
StudentController studentController= (StudentController) applicationContext.getBean("studentController");
studentController.setFunction("控制");
studentController.setName("CONTROLLER");
System.out.println(studentController);
}
}
Bean 概述
Property | Explained |
---|---|
class | 实例化 bean |
name | 命名 bean |
scope | Bean 范围 |
constructor arguments | 依赖注入 |
properties | 依赖注入 |
autowiring mode | 自动装配合作者 |
lazy-initialization mode | 延迟初始化的 bean |
initialization method | 初始化回调 |
destruction method | 销毁回调 |
- bean作用域
单例 原型 会话 请求 全局会话 应用程序 WebSocket 范围
也可以自定义 bean范围 需要实现org.springframework.beans.factory.config.Scope接口 - 自定义 bean 的性质
-
生命周期回调
要与容器对 bean 生命周期的 Management 进行交互,可以实现 Spring InitializingBean和DisposableBean接口。容器对前者调用afterPropertiesSet(),对后者调用destroy(),以允许 Bean 在初始化和销毁 Bean 时执行某些操作。从 Spring 2.5 开始,您具有三个用于控制 bean 生命周期行为的选项:InitializingBean和DisposableBean回调接口;自定义init()和destroy()方法;和@PostConstruct 和@PreDestroy 注解。您可以结合使用这些机制来控制给定的 bean。
- 其他感知接口
Name | Injected Dependency |
---|---|
ApplicationContextAware | 声明ApplicationContext |
ApplicationEventPublisherAware | 附件ApplicationContext的事件发布者 |
BeanClassLoaderAware | 类加载器,用于加载 Bean 类 |
BeanFactoryAware | 声明BeanFactory |
BeanNameAware | 声明 bean 的名称 |
BootstrapContextAware | 资源适配器BootstrapContext容器在其中运行。通常仅在支持 JCA 的ApplicationContext s 中可用 |
LoadTimeWeaverAware | 定义* weaver *以便在加载时处理类定义 |
MessageSourceAware | 解决消息的已配置策略(支持参数化和国际化) |
NotificationPublisherAware | Spring JMX 通知发布者 |
PortletConfigAware | 当前PortletConfig容器在其中运行。仅在可感知网络的 Spring ApplicationContext中有效 |
PortletContextAware | 当前PortletContext容器在其中运行。仅在可感知网络的 Spring ApplicationContext中有效 |
ResourceLoaderAware | 配置的加载器,用于对资源的低级访问 |
ServletConfigAware | 当前ServletConfig容器在其中运行。仅在可感知网络的 Spring ApplicationContext中有效 |
ServletContextAware | 当前ServletContext容器在其中运行。仅在可感知网络的 Spring ApplicationContext中有效 |
- 自定义容器
- 基于注解的容器配置
- @Required 适用于 bean 属性设置器方法
- @Autowired 按类型注入
- @PostConstruct 和@PreDestroy 创建 和销毁方法
- @Component
- @Service
- @Controller
- @Repository
- Spring MVC 的@RestControllerComments 由@Controller和@ResponseBody组成
- 使用@Inject 和@Named 进行依赖注入
10.@Bean 和@Configuration - 使用过滤器自定义扫描
@ComponentScan注解的* includeFilters 或 excludeFilters 参数(或component-scan元素的 include-filter 或 exclude-filter *子元素)。每个过滤器元素都需要type和expression属性
配置文件扫描 <context:component-scan base-package=“com.acme”/> - web.xml
<web-app>
<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified @Configuration classes. Fully-qualified packages may also be
specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.AppConfig</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Declare a Spring MVC DispatcherServlet as usual -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-delimited
and fully-qualified @Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.web.MvcConfig</param-value>
</init-param>
</servlet>
<!-- map all requests for /app/* to the dispatcher servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>