XML注入方式
1.set 方式注入 2.构造方式注入 3.工厂方式注入
set 方式注入
1.ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
2.public class UserDaoImpl implements UserDao;
3.private UserDao dao;
ClassPathXmlApplicationContext 创建了一个 spring 容器后,产生 UserDao 和 dao 对象,并且将 UserDao对象 设置到 dao 对象的属性中,这是通过 set 方法设置的
文件目录:
代码:配置文件: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="userDao" class="com.neuedu.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.neuedu.service.UserService">
<!-- set方法注入 -->
<property name="dao" ref="userDao"></property>
</bean>
</beans>
Test.java
package com.neuedu.test;
import com.neuedu.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//UserService us= (UserService)ac.getBean("userService");
}
}
UserDaoImpl.java
package com.neuedu.dao.impl;
import com.neuedu.dao.UserDao;
public class UserDaoImpl implements UserDao{
public UserDaoImpl() {
System.out.println("UserDaoImpl构造方法...");
}
public void save() {
System.out.println("通过oracle数据库将用户信息保存到数据库中");
}
}
UserService.java
package com.neuedu.service;
import com.neuedu.dao.UserDao;
import com.neuedu.dao.impl.UserDaoImpl;
public class UserService {
public UserService() {
System.out.println("UserService 构造方法...");
}
private UserDao dao;
public void save() {
dao.save();
}
public void setDao(UserDao dao) {
System.out.println("setUserDao...");
this.dao = dao;
}
}
UserDao.java
package com.neuedu.dao;
public interface UserDao {
public void save();
}
输出:
构造器方式注入(必须有带参数的构造方法才可以)
<?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="userDao" class="com.neuedu.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.neuedu.service.UserService">
<!-- set方法注入 -->
<!-- <property name="dao" ref="userDao"></property> -->
<!-- 构造方法注入 -->
<constructor-arg ref="userDao"></constructor-arg>
</bean>
</beans>
package com.neuedu.service;
import com.neuedu.dao.UserDao;
import com.neuedu.dao.impl.UserDaoImpl;
public class UserService {
private UserDao dao;
public UserService() {
System.out.println("UserService 构造方法...");
}
public UserService(UserDao dao) {
System.out.println("带参数的UserService");
this.dao = dao;
}
public void save() {
dao.save();
}
public void setDao(UserDao dao) {
System.out.println("setUserDao...");
this.dao = dao;
}
}
XML注入类型
1.简单属性 2. 对象 3.集合
简单属性
<?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="userDao" class="com.neuedu.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.neuedu.service.UserService">
<!-- set方法注入 -->
<!-- <property name="dao" ref="userDao"></property> -->
<!-- 构造方法注入 -->
<constructor-arg ref="userDao"></constructor-arg>
</bean>
<bean id="person" class="com.neudeu.po.Person">
<property name="name" value="zhang"></property>
<property name="age" value="30"></property>
</bean>
</beans>
package com.neuedu.test;
import com.neudeu.po.Person;
import com.neuedu.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//UserService us= (UserService)ac.getBean("userService");
Person person = ac.getBean(Person.class);
System.out.println(person);
}
}
Person.java
package com.neudeu.po;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [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">
<bean id="userDao" class="com.neuedu.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.neuedu.service.UserService">
<!-- set方法注入 -->
<!-- <property name="dao" ref="userDao"></property> -->
<!-- 构造方法注入 -->
<constructor-arg ref="userDao"></constructor-arg>
</bean>
<bean id="FLL" class="com.neudeu.po.Car">
<property name="brand" value="法拉利"></property>
<property name="price" value="3000000"></property>
</bean>
<bean id="BW" class="com.neudeu.po.Car">
<property name="brand" value="宝马"></property>
<property name="price" value="300000"></property>
</bean>
<bean id="BYD" class="com.neudeu.po.Car">
<property name="brand" value="比亚迪"></property>
<property name="price" value="30000"></property>
</bean>
<bean id="person" class="com.neudeu.po.Person">
<property name="name" value="zhang"></property>
<property name="age" value="30"></property>
<property name="cars">
<list>
<ref bean="FLL"/>
<ref bean="BW"/>
<ref bean="BYD"/>
</list>
</property>
</bean>
</beans>
package com.neudeu.po;
import java.util.List;
public class Person {
private String name;
private int age;
private List<Car> cars;
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Car.java
package com.neudeu.po;
public class Car {
private String brand;
private double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
<?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="userDao" class="com.neuedu.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.neuedu.service.UserService">
<!-- set方法注入 -->
<!-- <property name="dao" ref="userDao"></property> -->
<!-- 构造方法注入 -->
<!-- <constructor-arg ref="userDao"></constructor-arg> -->
<property name="dao">
<bean class="com.neuedu.dao.impl.UserDaoImpl"></bean>
</property>
</bean>
<bean id="FLL" class="com.neudeu.po.Car">
<property name="brand" value="法拉利"></property>
<property name="price" value="3000000"></property>
</bean>
<bean id="BW" class="com.neudeu.po.Car">
<property name="brand" value="宝马"></property>
<property name="price" value="300000"></property>
</bean>
<bean id="BYD" class="com.neudeu.po.Car">
<property name="brand" value="比亚迪"></property>
<property name="price" value="30000"></property>
</bean>
<bean id="person" class="com.neudeu.po.Person">
<property name="name" value="zhang"></property>
<property name="age" value="30"></property>
<property name="cars">
<list>
<bean class="com.neudeu.po.Car">
<property name="brand" value="红旗"></property>
<property name="price" value="300000"></property>
</bean>
<ref bean="FLL"/>
<ref bean="BW"/>
<ref bean="BYD"/>
</list>
</property>
</bean>
</beans>
package com.neuedu.test;
import com.neudeu.po.Person;
import com.neuedu.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}