Spring5学习1

Spring5学习1

Spring set注入

  1. 普通值注入 value

  2. bean注入 ref

  3. 数组

<property name="" >

   ​	<array>

   ​	<value></value>

   ​	<value></value>

   ​	</array>

   ​	</property>
  1. 集合
 <property name="" >

   ​	<list>

   ​	<value></value>

   ​	<value></value>

   ​	</list>

   ​	</property>
  1. map集合
<property name="" >

   ​	<map>

   ​	<entry key="" value=""/>

   ​	<entry key="" value=""/>

   ​	</map>

   ​	</property>
  1. set集合
<property name="" >

​	<set>

​	<value></value>

​	<value></value>

​	</set>

​	</property>
  1. null注入
  <property name="" >

   <null/>

   </property>
  1. properties
   <property name="" >

   <prop key="username">liritian</prop>

   <prop key="password">***********</prop>

   </property>

注意点:使用需要导入约束

p空间 就是property的缩写 直接注入属性的值

c空间 就是constructor的缩写 必须有有参构造

<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <bean id="address" class="cn.xiong.pojo.Address">
        <property name="address" value="西安"></property>
    </bean>

    <bean id="student" class="cn.xiong.pojo.student">
        <property name="name" value="李日天"></property>
        <property name="address" ref="address"></property>
        <property name="books">
            <array>
                <value>三国演义</value>
                <value>校花的贴身高手</value>
                <value>雍正王朝</value>
            </array>
        </property>
        <property name="hobbys">
            <list>
                <value>看小说</value>
                <value>看电影</value>
                <value>编程</value>
            </list>
        </property>
        <property name="card">
            <map>
                <entry key="wodiaonimade" value="茄子"></entry>
                <entry key="wozya" value="ququ"></entry>
            </map>
        </property>
        <property name="games">
            <set>
                <value>lol</value>
                <value>qqfeiche</value>
                <value>dnf</value>
            </set>
        </property>
        <property name="wife">
            <null/>
        </property>
        <property name="info">
           <props>
               <prop key="username">liritian</prop>
               <prop key="password">***</prop>
               <prop key="root">root</prop>
           </props>
        </property>
    </bean>
    <bean id="student1" class="cn.xiong.pojo.student" p:address-ref="address"></bean>
    <bean id="student1" class="cn.xiong.pojo.student" c:address-ref="address"></bean>

</beans>

测试:

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
student student = (student) context.getBean("student");
student student1 = (student) context.getBean("student1");
System.out.println(student.toString());
System.out.println(student1.toString());

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-I0Vrr9Eu-1620181683746)(C:\Users\优小熊Xx\AppData\Roaming\Typora\typora-user-images\1620025698832.png)]

自动装配:

​ 配置支持:

xmlns:context=“http://www.springframework.org/schema/context”
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
context:annotation-config/

byName,会自动在容器中查找和自己对象set方法后面的值相对应的beanid

hobby对象中有address和name的set方法所以会自动装配这两个的id

byType,会自动在容器中查找和自己对象属性类型相同的bean (必须保证类型全局唯一)

 <bean id="address" class="cn.xiong.pojo.Address"> </bean>

 <bean id="name" class="cn.xiong.pojo.name"> </bean> 

<bean id="hobby" class="cn.xiong.pojo.hobby" autowire="byType"></bean>

hobby中有与address和name中class类相同的属性所以会自动装配

@Autowired 直接在属性上或者set方法上使用即可,可以不要set方法

如果定义了Autowired的required属性为false,则说明这个对象可以为null,否则不允许为空,是通过byName实现

@Qualifier(value = “dog111”) 如果有多个对象不能实现自动装配,则可以添加该注解指定唯一的Bean对象注入

@Resource(name="")默认通过byName方式实现,如果找不到则通过byType属性实现

@Nullable 字段标记了这个属性则可以为null


<context:component-scan base-package=“cn.xiong.pojo”></context:component-scan>

扫描这个包后,可以不用在配置文件中写bean对象而是可以直接在类上使用@Component可以使用@Value为其set属性赋值

实体类

@Component
public class Cat {
    public void miao(){
        System.out.println("喵");
    }
}
public class Dog {
    public void wang(){
        System.out.println("汪");
    }
}
public class xiong {
    @Override
    public String toString() {
        return "xiong{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", Cat=" + cat +
                ", Dog=" + dog +
                '}';
    }

    private String name;
    private int  age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }
    @Autowired(required = false)
    private Cat cat;
    @Autowired(required = false)
    @Qualifier(value = "dog111")
    private Dog dog;
}

配置文件

<bean id="dog" class="cn.xiong.pojo.Dog"></bean>
<bean id="dog111" class="cn.xiong.pojo.Dog"></bean>
<bean id="xx" class="cn.xiong.pojo.xiong">
    <property name="name" value="liyixiong"></property>
    <property name="age" value="20"></property>
</bean>

测试类

public class mytest {
   @Test
    public void xiongTestA(){
       ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
       xiong xx = context.getBean("xx", xiong.class);
       System.out.println(xx.toString());
       xx.getCat().miao();
       xx.getDog().wang();
   }
}

作用域:@scope(“singleton”)单例…


javaconfig类替代xml文件

实体类

package cn.xiong.pojo;

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

/**
 * @Author:优小熊
 * @Description:Fightforyou
 */
@Component
public class User {
    public String getName() {
        return name;
    }
    @Value("嘻嘻嘻")
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }

    private String name;

}

配置文件

package cn.xiong.config;

import cn.xiong.pojo.User;
import org.springframework.context.annotation.*;

/**
 * @Author:优小熊
 * @Description:Fightforyou
 */
@Configuration
//本质上就是一个配置类,会被Spring容器托管,相当于Beans.xml
@Import(configclass1.class)
@ComponentScan(value = "cn.xiong.pojo")
public class configclass2 {
    @Bean
    //就相当于之前写的Bean标签,id就是方法名,class值就是 return返回的值
    public User getUser(){
        return new User();
    }
}

测试类

import cn.xiong.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Author:优小熊
 * @Description:Fightforyou
 */
public class mytest {
    public static void main(String[] args) {
        //如果完全使用了配置类方法去做,则需要通过AnnotationConfigApplicationContext上下文来获取容器,通过配置类的class对象加载
        ApplicationContext context = new AnnotationConfigApplicationContext("configclass2.class");
        User user = context.getBean("getUser", User.class);
        System.out.println(user.getName());
    }
}
ghtforyou
 */
public class mytest {
    public static void main(String[] args) {
        //如果完全使用了配置类方法去做,则需要通过AnnotationConfigApplicationContext上下文来获取容器,通过配置类的class对象加载
        ApplicationContext context = new AnnotationConfigApplicationContext("configclass2.class");
        User user = context.getBean("getUser", User.class);
        System.out.println(user.getName());
    }
}
上一篇:80后程序员感慨中年危机,javaredis队列实现高并发


下一篇:11. Spring5新特性