本文主要是介绍spring4.x依赖注入方法(DI),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.DI方法
属性注入(Setter方法注入)
构造器注入
工厂方法注入(很少使用,不推荐)
2.属性注入(Setter方法注入)
applicationContext-bean.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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><bean id="OutputHelper" class="cn.com.pojo.OutputHelper"><property name="outputGenerator"><ref bean="CsvOutputGenerator" /></property></bean><bean id="CsvOutputGenerator" class="cn.com.pojo.CsvOutputGenerator" /><bean id="JsonOutputGenerator" class="cn.com.pojo.JsonOutputGenerator" /></beans>
OutputHelper.java
package cn.com.pojo.output;import com.yiibai.output.IOutputGenerator;public class OutputHelper
{IOutputGenerator outputGenerator;public void setOutputGenerator(IOutputGenerator outputGenerator){this.outputGenerator = outputGenerator;}}
调用方法Main.java
package cn.com.pojo;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {//1. 创建 Spring 的 IOC 容器ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-bean.xml");//2. 从 IOC 容器中获取 bean 的实例OutputHelper outputHelper = (OutputHelper) ctx.getBean("OutputHelper");//根据类型来获取 bean 的实例: 要求在 IOC 容器中只有一个与之类型匹配的 bean, 若有多个则会抛出异常. //一般情况下, 该方法可用, 因为一般情况下, 在一个 IOC 容器中一个类型对应的 bean 也只有一个. // OutputHelper outputHelper = ctx.getBean(OutputHelper.class);//3. 使用 beanSystem.out.println(outputHelper)}
}
3.构造器注入
applicationContext-bean..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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><bean id="CustomerBean" class="cn.com.pojo.Customer"><!-- 应该为构造函数指定的确切数据类型--><constructor-arg type="java.lang.String"><value>yiibai</value></constructor-arg><constructor-arg type="java.lang.String"><value>188</value></constructor-arg><constructor-arg type="int"><value>28</value></constructor-arg></bean></beans>
Customer.java
package cn.com.pojo;public class Customer
{private String name;private String address;private int age;public Customer(String name, String address, int age) {this.name = name;this.address = address;this.age = age;}public Customer(String name, int age, String address) {this.name = name;this.age = age;this.address = address;}//getter and setter methodspublic String toString(){return " name : " +name + "\n address : "+ address + "\n age : " + age;}}
运行类:App .java
package cn.com.pojo;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App
{public static void main( String[] args ){ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});Customer cust = (Customer)context.getBean("CustomerBean");System.out.println(cust);// name : yiibai// address : 28// age : 188}
}
4工厂方法注入(很少使用,不推荐)
这篇关于spring4.x依赖注入方法(DI)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!