Spring_初识配置+DI
第一个spring程序配置流程
先导入依赖
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.3.24</version>
      </dependency>
实体类
public class Hello {
    private String str;
    public String getStr() {
        return str;
    }
    public void setStr(String str) {
        this.str = str;
    }
    @Override
    public String toString() {
        return "Hello{" +
                "str='" + str + '\'' +
                '}';
    }
}
applicationContext.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    使用Spring来创建对象,在Spring中,这些都称为Bean
        类型 变量名 = new 类型()
        Hello hello =new Hello()
        bean = 对象   new hello();
        id=变量名
        class=new 对象
        property 相当于给对象中属性赋值
-->
    <bean id="hello" class="com.xiaofan.pojo.Hello">
        <property name="str" value="Spring"/>
    </bean>
</beans>
public class MyTest {
    @Test
    public void test(){
        //获取spring的上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //我们的对象现在都在spring中的管理了,我们要使用,直接去里面取出来就可以
        Hello hello = (Hello) context.getBean("hello");
        System.out.println(hello.toString());
    }
}

构造器注入
实体类
package com.xiaofan.pojo;
public class User {
    private String name;
    public User(){
        System.out.println("User的无参构造");
    }
    public User(String name){
        this.name =name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void show(){
        System.out.println("name:"+name);
    }
}
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    第一种,下标赋值-->
<!--    <bean id="user" class="com.xiaofan.pojo.User">-->
<!--        <constructor-arg index="0" value="小凡"/>-->
<!--    </bean>-->
<!--    不建议使用,如果两个参数都是String?-->
<!--    <bean id="user" class="com.xiaofan.pojo.User">-->
<!--        <constructor-arg type="java.lang.String" value="xiaofan"/>-->
<!--    </bean>-->
<!--    直接通过参数名来设置-->
    <bean id="user" class="com.xiaofan.pojo.User">
        <constructor-arg name="name" value="xiaofan"/>
    </bean>
</beans>
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context =new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) context.getBean("user");
        user.show();
    }
}
DI(依赖注入)
实体类
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String ,String > card;
    private Set<String> games;
    private Properties info;
    private String wife;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public String[] getBooks() {
        return books;
    }
    public void setBooks(String[] books) {
        this.books = books;
    }
    public List<String> getHobbies() {
        return hobbies;
    }
    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }
    public Map<String, String> getCard() {
        return card;
    }
    public void setCard(Map<String, String> card) {
        this.card = card;
    }
    public Set<String> getGames() {
        return games;
    }
    public void setGames(Set<String> games) {
        this.games = games;
    }
    public Properties getInfo() {
        return info;
    }
    public void setInfo(Properties info) {
        this.info = info;
    }
    public String getWife() {
        return wife;
    }
    public void setWife(String wife) {
        this.wife = wife;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", info=" + info +
                ", wife='" + wife + '\'' +
                '}';
    }
}
public class Address {
    private String address;
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="address" class="com.xiaofan.pojo.Address">
        <property name="address" value="西安"/>
    </bean>
    <bean id="student" class="com.xiaofan.pojo.Student">
<!--        第一种,普通值注入,value-->
        <property name="name" value="小凡"/>
<!--        第二种,bean注入,ref-->
        <property name="address" ref="address"/>
<!--        数组注入-->
        <property name="books">
            <array>
                <value>西游记</value>
                <value>三国演义</value>
                <value>水浒传</value>
                <value>红楼梦</value>
            </array>
        </property>
<!--        list注入-->
        <property name="hobbies">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
            </list>
        </property>
<!--        map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="1234567898765433456"/>
                <entry key="银行卡" value="3245678987654345765"/>
            </map>
        </property>
<!--        set注入-->
        <property name="games">
            <set>
                <value>王者荣耀</value>
                <value>LOL</value>
            </set>
        </property>
<!--        null注入-->
        <property name="wife">
            <null/>
        </property>
<!--        Properties注入,特殊类型-->
        <property name="info">
            <props>
                <prop key="driver">com.mysql.jdbc.Driver</prop>
                <prop key="url">jdbc:mysql://localhost:8080/spring</prop>
                <prop key="username">xiaofan</prop>
                <prop key="password">123456</prop>
            </props>
        </property>
     </bean>
</beans>
    @Test
    public void test(){
        ApplicationContext context =new ClassPathXmlApplicationContext("beans.xml");
        Student student =(Student) context.getBean("student");
        System.out.println(student.toString());
    }
命名空间注入
实体类
public class User {
    private String name;
    private int age;
    public User() {
    }
    public User(String name, int age) {
        this.name = name;
        this.age = 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;
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--    p命名空间注入,可以直接注入属性的值:property-->
    <bean id="user" class="com.xiaofan.pojo.User" p:name="小凡" p:age="18"/>
<!--    c命名空间注入,通过构造器注入:construct-args-->
    <bean id="user2" class="com.xiaofan.pojo.User" c:age="19" c:name="tata"/>
</beans>
    @Test
    public void test2(){
        ApplicationContext context =new ClassPathXmlApplicationContext("userbeans.xml");
        User user = context.getBean("user2", User.class);
        System.out.println(user);
    }
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号