Spring 依赖注入

来源:互联网 发布:51单片机毕业设计题库 编辑:程序博客网 时间:2024/06/06 10:01

– Start
对象都不是孤立存在的,它们之间有依赖关系,以此来协同工作,Spring 支持基于构造方法和 setter 方法两种依赖注入方式。

基于构造器的依赖注入

基于构造器的依赖注入非常简单,下面是一个简单的例子。注意 Spring 通过类型匹配参数,如果构造器有多个类型相同的参数,我们可以通过 index 指定参数序号。

package shangbo.spring.core.example19;public class Person {    private String name;    private int age;    public Person(String name, int age) {        this.name = name;        this.age = age;    }    public String toString() {        return "[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"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd">    <!--         构造器方式依赖注入        index 指定参数序号        value 用来注入基本类型,如:String, Integer, Double 等     -->    <bean class="shangbo.spring.core.example19.Person">        <constructor-arg index="0" value="shangbo"/>        <constructor-arg index="1" value="30"/>    </bean></beans>
package shangbo.spring.core.example19;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {    public static void main(String[] args) {        // 实例化 Spring IoC 容器        ApplicationContext context = new ClassPathXmlApplicationContext("example.xml", Person.class);        // 从容器中获得 Person 对象        Person p = context.getBean(Person.class);        // 使用对象        System.out.println(p);    }}

我们也可以使用 c 命名空间方式注入bean,更简洁。

<?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:c="http://www.springframework.org/schema/c"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <!--         构造器方式依赖注入        使用 c 命名空间        注意需要引入 c 命名空间     -->    <bean id="shangbo" class="shangbo.spring.core.example40.Person"        c:name="shangbo"        c:age="30"/></beans>

基于 Setter 方法的依赖注入

package shangbo.spring.core.example20;public class Person {    private String name;    private int age;    //    // Setter    //    public void setName(String name) {        this.name = name;    }    public void setAge(int age) {        this.age = age;    }    public String toString() {        return "[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"    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd">    <!--         基于 Setter 方法的依赖注入        name 指定对象属性名字        value 用来注入基本类型,如:String, Integer, Double 等     -->    <bean class="shangbo.spring.core.example20.Person">        <property name="name" value = "shangbo"/>        <property name="age" value = "30"/>    </bean></beans>
package shangbo.spring.core.example20;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {    public static void main(String[] args) {        // 实例化 Spring IoC 容器        ApplicationContext context = new ClassPathXmlApplicationContext("example.xml", Person.class);        // 从容器中获得 Person 对象        Person p = context.getBean(Person.class);        // 使用对象        System.out.println(p);    }}

我们也可以使用 p 命名空间方式注入bean,更简洁。

<?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"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <!--         基于 Setter 方法的依赖注入        使用 p 命名空间        注意需要引入 p 命名空间     -->    <bean id="shangbo" class="shangbo.spring.core.example41.Person"        p:name="shangbo"        p:age="30"/></beans>

– 更多参见:Spring Framework 精萃
– 声 明:转载请注明出处
– Last Updated on 2017-06-17
– Written by ShangBo on 2017-05-22
– End

原创粉丝点击