spring学习之注入依赖

来源:互联网 发布:义乌美工培训ywxdf 编辑:程序博客网 时间:2024/06/04 19:05

简单的总结一下学习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:jdbc="http://www.springframework.org/schema/jdbc"       xmlns:jee="http://www.springframework.org/schema/jee"       xmlns:p="http://www.springframework.org/schema/p"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:util="http://www.springframework.org/schema/util"       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/jdbc        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd         http://www.springframework.org/schema/jee        http://www.springframework.org/schema/jee/spring-jee.xsd         http://www.springframework.org/schema/util        http://www.springframework.org/schema/util/spring-util.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd">    <context:component-scan base-package="springmvc,test"/>    <!--    <context:property-placeholder location="classpath:a.properties"></context:property-placeholder>    -->    <util:list id="list">        <value>人民的名义</value>        <value>临高齐名</value>    </util:list></beans>

在这个配置文件中,有一个自动化装配bean的标签,这个标签会扫描指定的包中的类,凡是含有@Component注解都会自动创建bean.

<context:component-scan base-package="springmvc,test"/>

比如

@Component("aaaa")@Qualifier("huawei")@PropertySource("classpath:a.properties")public class A {    @Value("${name}")    private String name;    @Override    public String toString() {        return "A{" +                "name='" + name + '\'' +                '}';    }}

扫描到这个类的时候,会创建一个A类的对象bean,名字是aaaa.
创建了bean之后,就要把这些bean装配起来,已经创建的对象之间的引用,可以直接@Autowired注入.
但是总有一些处于底层的bean,他们被其它bean引用,自己的属性要通过value获取.

@Component("aaaa")@Qualifier("huawei")@PropertySource("classpath:a.properties")public class A {    @Value("${name}")    private String name;    @Override    public String toString() {        return "A{" +                "name='" + name + '\'' +                '}';    }}

这个是通过properties配置文件给定值.

  @Value("#{@list}")    private List<String>  car;

这个是xml文件获取.两个语法有些不一样.

另外需要注意的就是路径.

package test;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import org.springframework.stereotype.Component;import springmvc.A;import springmvc.B;import java.io.File;@Component("ttttt")public class TestSpring {    @Test    public void test01(){       // ApplicationContext context=new FileSystemXmlApplicationContext("applicationContext.xml");        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");        int n=context.getBeanDefinitionCount();        String[] ss=context.getBeanDefinitionNames();        for (String s:ss) {            System.out.println(s);        }        System.out.println("n= "+n);        A a= context.getBean(A.class);        System.out.println(a);        B b=context.getBean(B.class);        System.out.println(b);    }}

这个 ClassPathXmlApplicationContext的路径是src文件下的,要注意.
properties属性配置文件可以有多个,我用了两个,在

@PropertySource("classpath:b.properties")

这里面指定就可以.

spring的自动装配,依赖注入非常象资本家和劳工的关系.单个的劳工都是单独招聘的,互相之间的关系不是自己决定的,spring中的bean会有一个层级,就像是树一样,上面的bean引用下面的bean,层层引用,处在叶子节点的bean的值没有办法引用其它的bean,要获取Value初始化自己的属性,这个Value可以从xml文件获取,也可以从properties文件获取,两个语法有一些不一样.

@Value("#{@list}")private List<String>  car;@Value("${name}")private String name;
原创粉丝点击