一路繁花似锦绣前程
失败的越多,成功才越有价值

导航

 

一、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>A02spring</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--https://mvnrepository.com/artifact/org.springframework/spring-context-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

二、spring的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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.wuxi"></context:component-scan>

</beans>

三、bean

package com.wuxi.benas;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/*
一、注解创建对象(四个注解功能一样)
    @Component:属性value用于指定bean的id,它的默认值是当前类名(首字母小写)
    @Controller:一般用在表现层
    @Service:一般用在业务层
    @Repository:一般用在持久层
二、注入数据(注解无法注入集合类型数据,只有xml方式可以注入)
    @Autowired
        @Autowired
        private Student student;
        1、容器中bean的id作为key,class作为value
        2、注入时,首先会在容器中匹配数据类型(Student和class)
        3、当有多个数据类型可匹配时,则匹配变量名称(student和id)
    @Qualifier
        @Autowired
        @Qualifier("studentAnno")
        private Student student;
        1、根据@Qualifier的value值,匹配容器中的id
        2、必须和@Autowired一起使用
    @Resource
        @Resource(name="studentAnno")
        private Student student;
        1、相当于@Autowired+@Qualifier
    @Value
        @Value("mengmeiqi")
        private String name;
        1、可以注入String类型或者基本数据类型的数据,以上三种无法注入
        2、属性值可以使用${}表达式,来注入配置文件中的数据
3、创建对象方式
    @Scope
        singleton:单例;生命周期与容器一致
        prototype:多例;使用时创建。没有引用时,由垃圾回收器销毁
4、生命周期回调
    @PostConstruct:对象创建回调方法
    @PreDestroy:对象销毁回调方法
*/
@Component("gradeAnno")
@Scope("singleton")
public class Grade {

    @Resource(name="studentAnno")
    private Student student;

    @PostConstruct
    public void initMethod(){
        System.out.println("对象创建");
    }

    @PreDestroy
    public void destoryMethod(){
        System.out.println("对象销毁");
    }

    public void studentMethod() {
        student.study();
    }
}

四、使用

package com.wuxi.tests;

import com.wuxi.benas.Grade;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MySpringTest {
    public static void main(String[] args) {
        // ApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
/*
        // 测试注解
        Grade grade = ac.getBean("gradeAnno", Grade.class);
        grade.studentMethod();

        // 测试对象创建方式
        Grade grade1 = ac.getBean("gradeAnno", Grade.class);
        Grade grade2 = ac.getBean("gradeAnno", Grade.class);
        System.out.println(grade1 == grade2);
*/
        // 测试生命周期回调
        ac.getBean("gradeAnno", Grade.class);
        ac.close();
    }
}

 

posted on 2020-09-12 17:33  一路繁花似锦绣前程  阅读(124)  评论(0编辑  收藏  举报