spring-03 DI

基于XML的DI  

所谓注入,可理解为对象的属性赋值  

设值注入  

  1. P命名空间注入  
  2. 集合属性注入  
  3. 域属性自动注入(byName\byType)  
  4. 构造注入

基于注解的DI

  1. 环境搭建
  2. 常用注解:@Component、@Scope、@Value、 @Resource、@Autowired 

 

1.普通的一个注入演示

school.java

public class School {
	private String name;

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "School [name=" + name + "]";
	}
	
}

Student.java

public class Student {
	private String name;
	private int age;
	private School school;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		System.out.println("setName()方法执行!");
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		System.out.println("setAge()方法执行!");
		this.age = age;
	}
	
	public School getSchool() {
		return school;
	}
	public void setSchool(School school) {
		this.school = school;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
	}
	
	
}

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 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
        
    <bean id="student" class="com.bjsxt.pojo.Student">
    	<property name="name" value="张三"/>
    	<property name="age" value="23"/>
    	<property name="school" ref="mySchool"/>
    </bean>
    
    <bean id="mySchool" class="com.bjsxt.pojo.School">
    	<property name="name" value="清华大学"/>
    </bean>
    
</beans>

执行结果

202024_YP21_3519338.png

2.构造注入

School.java

public class School {
	private String name;

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "School [name=" + name + "]";
	}
	
}

Student.java

public class Student {
	private String name;
	private int age;
	private School school;
	
	
	public Student() {
	}

	public Student(String name, int age, School school) {
		super();
		this.name = name;
		this.age = age;
		this.school = school;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
	}
	
	
}

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 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
        
    <bean id="student" class="com.bjsxt.pojo.Student">
    	
    	<constructor-arg name="name" value="李四"/>
    	<constructor-arg name="age" value="24"/>
    	<constructor-arg name="school" ref="mySchool"/>
    	
    	 <!-- 
    	<constructor-arg index="0" value="李四"/>
    	<constructor-arg index="1" value="24"/>
    	<constructor-arg index="2" ref="mySchool"/>
    	  -->
    	<!-- 
    	<constructor-arg value="李四"/>
    	<constructor-arg value="24"/>
    	<constructor-arg ref="mySchool"/>
    	 -->
    </bean>
    
    <bean id="mySchool" class="com.bjsxt.pojo.School">
    	<property name="name" value="清华大学"/>
    </bean>
    
</beans>

测试代码

public final class TestStudent {
	@Test
	public void test01(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Student result = (Student) ac.getBean("student");
		System.out.println(result);
	}
}

202333_bIaN_3519338.png

3.P命名空间注入

增加xmlns:p="http://www.springframework.org/schema/p"

School.java

public class School {
	private String name;

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "School [name=" + name + "]";
	}
	
}

Student.java

public class Student {
	private String name;
	private int age;
	private School school;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		System.out.println("setName()方法执行!");
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		System.out.println("setAge()方法执行!");
		this.age = age;
	}
	
	public School getSchool() {
		return school;
	}
	public void setSchool(School school) {
		this.school = school;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
	}
	
	
}

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"
    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">
        
    <bean id="student" class="com.bjsxt.pojo.Student" p:name="王五" p:age="25" p:school-ref="mySchool"/>
    
    <bean id="mySchool" class="com.bjsxt.pojo.School">
    	<property name="name" value="清华大学"/>
    </bean>
    
</beans>

测试代码

public final class TestStudent {
	@Test
	public void test01(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Student result = (Student) ac.getBean("student");
		System.out.println(result);
	}
}

202521_i3EK_3519338.png

4.集合注入

Some.java

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Some {
	private String[] arrays;
	private Set<School> mySets;
	private List<String> myLists;
	private Map<String,String> myMaps;
	private Properties myProps;
	public String[] getArrays() {
		return arrays;
	}
	public void setArrays(String[] arrays) {
		this.arrays = arrays;
	}
	public Set<School> getMySets() {
		return mySets;
	}
	public void setMySets(Set<School> mySets) {
		this.mySets = mySets;
	}
	public List<String> getMyLists() {
		return myLists;
	}
	public void setMyLists(List<String> myLists) {
		this.myLists = myLists;
	}
	public Map<String, String> getMyMaps() {
		return myMaps;
	}
	public void setMyMaps(Map<String, String> myMaps) {
		this.myMaps = myMaps;
	}
	public Properties getMyProps() {
		return myProps;
	}
	public void setMyProps(Properties myProps) {
		this.myProps = myProps;
	}
	@Override
	public String toString() {
		return "Some [arrays=" + Arrays.toString(arrays) + ", mySets=" + mySets + ", myLists=" + myLists + ", myMaps="
				+ myMaps + ", myProps=" + myProps + "]";
	}
	
	
}

School.java

public class School {
	private String name;

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "School [name=" + name + "]";
	}
	
}

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 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
        
    
    <bean id="mySchool1" class="com.bjsxt.pojo.School">
    	<property name="name" value="清华大学"/>
    </bean>
    <bean id="mySchool2" class="com.bjsxt.pojo.School">
    	<property name="name" value="北京大学"/>
    </bean>
    
    <bean id="some" class="com.bjsxt.pojo.Some">
    	<property name="arrays">
    		<array>
    			<value>北京</value>
    			<value>上海</value>
    			<value>天津</value>
    		</array>
    	</property>
    	<property name="mySets">
    		<set>
    			<ref bean="mySchool1"/>
    			<ref bean="mySchool2"/>
    		</set>
    	</property>
    	<property name="myLists">
    		<list>
    			<value>张三</value>
    			<value>李四</value>
    			<value>王五</value>
    		</list>
    	</property>
    	<property name="myMaps">
    		<map>
    			<entry key="QQ" value="2797577724"/>
    			<entry key="mobile" value="13333333333"/>
    		</map>
    	</property>
    	<property name="myProps">
    		<props>
    			<prop key="email">222@qq.com</prop>
    			<prop key="wechat">victor</prop>
    		</props>
    	</property>
    </bean>
    
</beans>

测试代码

public final class TestStudent {
	@Test
	public void test01(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Some result = (Some) ac.getBean("some");
		System.out.println(result);
	}
}

结果:

Some [arrays=[北京, 上海, 天津], mySets=[School [name=清华大学], School [name=北京大学]], myLists=[张三, 李四, 王五], myMaps={QQ=2797577724, mobile=13333333333}, myProps={email=222@qq.com, wechat=victor}]
 

 

5.域属性自动注入

5.1byName方式

School.java

public class School {
	private String name;

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "School [name=" + name + "]";
	}
	
}

Student.java

public class Student {
	private String name;
	private int age;
	private School school;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		System.out.println("setName()方法执行!");
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		System.out.println("setAge()方法执行!");
		this.age = age;
	}
	
	public School getSchool() {
		return school;
	}
	public void setSchool(School school) {
		this.school = school;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
	}
	
	
}

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 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
     <!-- 域属性自动注入:byName方式:注入的bean的id与被注入的对象的属性名称一致 -->
    <bean id="student" class="com.bjsxt.pojo.Student" autowire="byName">
    	<property name="name" value="张三"/>
    	<property name="age" value="23"/>
    </bean>
    
    <bean id="school" class="com.bjsxt.pojo.School">
    	<property name="name" value="清华大学"/>
    </bean>
    
</beans>

测试代码

public final class TestStudent {
	@Test
	public void test01(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Student result = (Student) ac.getBean("student");
		System.out.println(result);
	}
}

结果

203339_GcPQ_3519338.png

5.2 byType方式

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 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
     <!-- 域属性自动注入:byType方式:注入的bean所在的类没有其他继承或实现关系的类。-->
    <bean id="student" class="com.bjsxt.pojo.Student" autowire="byType">
    	<property name="name" value="张三"/>
    	<property name="age" value="23"/>
    </bean>
    
    <bean id="mySchool" class="com.bjsxt.pojo.School">
    	<property name="name" value="清华大学"/>
    </bean>
   
    
</beans>

6.基于注解的DI

所需的包

  1. com.springsource.org.apache.commons.logging-1.1.1.jar
  2. com.springsource.org.apache.log4j-1.2.15.jar
  3. junit-4.9.jar
  4. spring-aop-4.1.6.RELEASE.jar
  5. spring-beans-4.1.6.RELEASE.jar
  6. spring-context-4.1.6.RELEASE.jar
  7. spring-core-4.1.6.RELEASE.jar
  8. spring-expression-4.1.6.RELEASE.jar

1.基本的演示

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"
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd"> 
    <!-- spring的组件扫描器 -->
	<context:component-scan base-package="com.bjsxt.pojo"/>
</beans>
        
    

 School.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/*
 * 与@Component具有相同功能的注解还有其他三个:
 * @Repository:放在Dao实现类上
 * @Service:放在Service实现类上
 * @Controller:放在Controller上(SpringMVC)
 */

@Component("mySchool")    //代表该类是一个组件
@Scope(value="prototype")
public class School {
	@Value("清华大学")
	private String name;

	public String getName() {
		return name;
	}

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

	@Override
	public String toString() {
		return "School [name=" + name + "]";
	}
	
}

Student.java

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

@Component("myStudent")
@Scope("prototype")
public class Student {
	private String name;
	@Value("22")
	private int age;
    //@Resource //域属性注入:byType方式
    //@Resource(name="mySchool") //域属性注入:byName方式
    //@Autowired //域属性注入:byType方式
    @Autowired
	@Qualifier("mySchool")//域属性注入:byName方式
	private School school;
	public String getName() {
		return name;
	}
	@Value("zhangsan")
	public void setName(String name) {
		System.out.println("setName()方法执行!");
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		System.out.println("setAge()方法执行!");
		this.age = age;
	}
	
	public School getSchool() {
		return school;
	}
	public void setSchool(School school) {
		this.school = school;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
	}
	
	
}

2.get set方法

@Component("myStudent")
@Scope("prototype")
public class Student {
	@Value("zhangsan")
	private String name;
	@Value("22")
	private int age;
	@Autowired
	@Qualifier("mySchool")//域属性注入:byName方式
	private School school;
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
	}
	
	
}

/*
 * 与@Component具有相同功能的注解还有其他三个:
 * @Repository:放在Dao实现类上
 * @Service:放在Service实现类上
 * @Controller:放在Controller上(SpringMVC)
 */

@Component("mySchool")    //代表该类是一个组件
@Scope(value="prototype")
public class School {
	@Value("清华大学")
	private String name;


	@Override
	public String toString() {
		return "School [name=" + name + "]";
	}
	
}

3.xml

如果在bean中设置了值则实例化的对象为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:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd"> 
    <!-- spring的组件扫描器 -->
	<context:component-scan base-package="com.bjsxt.pojo"/>
	
	<bean id="myStudent" class="com.bjsxt.pojo.Student">
		<property name="name" value="lisi"/>
	</bean>
</beans>
        

 

转载于:https://my.oschina.net/nan99/blog/1499013

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值