2021-08-02 Spring温习(二 IoC 容器) 持续更新...

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 概述

PropertyExplained
class实例化 bean
name命名 bean
scopeBean 范围
constructor arguments依赖注入
properties依赖注入
autowiring mode自动装配合作者
lazy-initialization mode延迟初始化的 bean
initialization method初始化回调
destruction method销毁回调
  • bean作用域
    单例 原型 会话 请求 全局会话 应用程序 WebSocket 范围
    也可以自定义 bean范围 需要实现org.springframework.beans.factory.config.Scope接口
  • 自定义 bean 的性质
  1. 生命周期回调
    要与容器对 bean 生命周期的 Management 进行交互,可以实现 Spring InitializingBean和DisposableBean接口。容器对前者调用afterPropertiesSet(),对后者调用destroy(),以允许 Bean 在初始化和销毁 Bean 时执行某些操作。

    从 Spring 2.5 开始,您具有三个用于控制 bean 生命周期行为的选项:InitializingBean和DisposableBean回调接口;自定义init()和destroy()方法;和@PostConstruct 和@PreDestroy 注解。您可以结合使用这些机制来控制给定的 bean。

  • 其他感知接口
NameInjected Dependency
ApplicationContextAware声明ApplicationContext
ApplicationEventPublisherAware附件ApplicationContext的事件发布者
BeanClassLoaderAware类加载器,用于加载 Bean 类
BeanFactoryAware声明BeanFactory
BeanNameAware声明 bean 的名称
BootstrapContextAware资源适配器BootstrapContext容器在其中运行。通常仅在支持 JCA 的ApplicationContext s 中可用
LoadTimeWeaverAware定义* weaver *以便在加载时处理类定义
MessageSourceAware解决消息的已配置策略(支持参数化和国际化)
NotificationPublisherAwareSpring 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值