您的位置:首页 > 编程语言 > Java开发

《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]]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java Spring