《Spring 系列》- 依赖注入
2018-01-18 00:00
495 查看
项目结构
build.gradle
apply plugin:'java' repositories{ maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/' } } dependencies{ compile group: 'org.springframework', name: 'spring-context', version: '3.2.3.RELEASE' }
构造函数注入
applicationContext.xml<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="userId" class="com.kimisme.constructor.User"> <constructor-arg index="0" type="java.lang.String" value="jack"></constructor-arg> <constructor-arg index="1" type="java.lang.Integer" value="18"></constructor-arg> </bean> </beans>
User.java
public class User { private Integer uid; private String username; private Integer age; public User(String username, Integer age) { super(); this.username = username; this.age = age; }
Program.java
public class Program { public static void main(String[] args) { String xmlPath="com/kimisme/constructor/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); User user = applicationContext.getBean("userId",User.class); System.out.println(user); } }
输出
User [uid=null, username=jack, age=18]
属性注入
applicationContext.xml<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="personId" class="com.kimisme.property.Person"> <property name="name" value="jack"></property> <property name="age"> <value>18</value> </property> <property name="homeAddr" ref="homeAddrId"></property> <property name="companyAddr"> <ref bean="companyAddrId"/> </property> </bean> <bean id="homeAddrId" class="com.kimisme.property.Address"> <property name="addr" value="温州"></property> <property name="tel" value="10086"></property> </bean> <bean id="companyAddrId" class="com.kimisme.property.Address"> <property name="addr" value="杭州"></property> <property name="tel" value="100001"></property> </bean> </beans>
Address.java
public class Address { private String addr; private String tel; @Override public String toString() { return "Address [addr=" + addr + ", tel=" + tel + ", getAddr()=" + getAddr() + ", getTel()=" + getTel() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]"; }
Person.java
public class Person { private String name; private Integer age; private Address homeAddr; private Address companyAddr; @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", homeAddr=" + homeAddr + ", companyAddr=" + companyAddr + "]"; }
Program.java
public class Program { public static void main(String[] args) { String xmlPath="com/kimisme/property/applicationContext.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); Person person = applicationContext.getBean("personId",Person.class); System.out.println(person); } }
输出
Person [name=jack, age=18, homeAddr=Address [addr=温州, tel=10086, getAddr()=温州, getTel()=10086, getClass()=class com.kimisme.property.Address, hashCode()=156476729, toString()=com.kimisme.property.Address@953a539], companyAddr=Address [addr=杭州, tel=100001, getAddr()=杭州, getTel()=100001, getClass()=class com.kimisme.property.Address, hashCode()=1384400375, toString()=com.kimisme.property.Address@528445f7]]
相关文章推荐
- Spring整理系列(02)——spring依赖注入,组装对象之间的依赖关系
- SpringBoot系列: 理解 Spring 的依赖注入(二)
- Spring 的本质系列(1) :依赖注入
- Spring应用、原理以及粗读源码系列(一)--框架总述、以Bean为核心的机制(IoC容器初始化以及依赖注入)
- 日常阅读-Spring 的本质系列(1) :依赖注入
- Spring系列之-Spring IOC容器设计:依赖注入设计
- spring回顾系列:依赖注入
- Spring系列之依赖注入的方式
- Spring系列之依赖注入的方式
- SpringBoot系列: 理解 Spring 的依赖注入(一)
- Ioc容器依赖注入-Spring 源码系列(2)
- Spring系列之依赖注入的方式
- EJB系列(四)——Spring依赖注入 && EJB依赖注入
- 【SSH系列】深入浅出spring IOC中三种依赖注入方式
- Spring系列之 (五):依赖注入的方式
- Spring系列之四——Spring IOC依赖注入
- SPRING系列一之 依赖注入
- 【SSH系列】深入浅出spring IOC中三种依赖注入方式
- spring系列(二):依赖注入
- Spring.NET 1.3.1 新特性探索系列1——ASP.NET MVC的依赖注入