Sping4之注入参数

Posted wo卜配

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sping4之注入参数相关的知识,希望对你有一定的参考价值。

Spring的依赖注入不仅可以注入基本类型,也可以注入包括model,list等等类型

package com.hongcong.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hongcong.model.People;

public class test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ca = new  ClassPathXmlApplicationContext("beans.xml");
        //Bean注入
        People people1 = (People)ca.getBean("people1");
        System.out.println(people1);
        //内部bean
        People people2 = (People)ca.getBean("people2");
        System.out.println(people2);
        //null注入
        People people3 = (People)ca.getBean("people3");
        System.out.println(people3);
        //集合注入
        People people4 = (People)ca.getBean("people4");
        System.out.println(people4);
        
        
    }
}

beans.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="dog" class="com.hongcong.model.Dog">
        <property name="name" value="Jack"/>
    </bean>
    
    <!--Bean注入  -->
    <bean id="people1" class="com.hongcong.model.People">
        <property name="id" value="1"/>
        <property name="name" value="小一"/>
        <property name="age" value="11"/>
        <property name="dog" ref="dog"></property>
    </bean>
    
    <!--内部bean  -->
    <bean id="people2" class="com.hongcong.model.People">
        <property name="id" value="1"/>
        <property name="name" value="小一"/>
        <property name="age" value="11"/>
        <property name="dog">
            <bean class="com.hongcong.model.Dog" >
                <property name="name" value="Tom"></property>
            </bean>
        </property>
    </bean>
    
    <!--null注入  -->
    <bean id="people3" class="com.hongcong.model.People">
        <property name="id" value="1"/>
        <property name="name" value="小一"/>
        <property name="age" value="11"/>
        <property name="dog" >
            <null></null>
        </property>
    </bean>
    
    <!--list注入  -->
    <bean id="people4" class="com.hongcong.model.People">
        <property name="id" value="1"/>
        <property name="name" value="小一"/>
        <property name="age" value="11"/>
        <property name="loves" >
            <list>
                <value>唱歌</value>
                <value>听歌</value>
            </list>
        </property>
    </bean>
</beans>

people

package com.hongcong.model;

import java.util.ArrayList;
import java.util.List;

public class People {
    private int id;
    private String name;
    private int age;
    private Dog dog;
    List<String> loves = new ArrayList<String>();
    
    public People() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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 Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public List<String> getLoves() {
        return loves;
    }

    public void setLoves(List<String> loves) {
        this.loves = loves;
    }

    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + ", age=" + age
                + ", dog=" + dog + ", loves=" + loves + "]";
    }

     
}

dog

package com.hongcong.model;

public class Dog {
    private String name;

    public String getName() {
        return name;
    }

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

 

以上是关于Sping4之注入参数的主要内容,如果未能解决你的问题,请参考以下文章

以下代码片段是不是容易受到 Rails 5 中 SQL 注入的影响?

sql注入与防止SQL注入之参数化处理

JAVA之AOP

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段

MyBatis如何防止SQL注入