setter 注入方式
构造器注入
在实现类中添加简单数据类型,并提供 setter 方法
public class BookDaoImpl implements BookDao {
//注入简单类型
private int num;
public void setNum(int num) {
this.num = num;
}
@Override
public void save() {
System.out.println("book dao save......"+num);
}
}
配置文件中添加 bean
<bean id="bookDao" class="com.my.dao.impl.BookDaoImpl">
<property name="num" value="10"/>
bean>
测试运行结果
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationConfig.xml");
BookDao bookDao = (BookDao) context.getBean("bookDao");
bookDao.save();
//book dao save......10
}
在实现类中添加引用数据类型,并提供 setter 方法
public class BookServiceImpl implements BookService {
//注入引用类型
private BookDao bookDao;
@Override
public void save() {
System.out.println("book service save......");
bookDao.save();
}
//提供对应的set方法
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
}
配置文件中添加 bean
<bean id="bookDao" class="com.my.dao.impl.BookDaoImpl">
<property name="connectionNum" value="10"/>
bean>
<bean id="bookService" class="com.my.service.impl.BookServiceImpl">
//引用类型
<property name="bookDao" ref="bookDao"/>
bean>
测试运行结果
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationConfig.xml");
BookService bookService = (BookService) context.getBean("bookService");
bookService.save();
//输出结果为: book service save......
//book dao save......10
}
在实现类中,添加构造方法
public class BookDaoImpl implements BookDao {
private int num;
public BookDaoImpl(int num) {
this.num = num;
}
@Override
public void save() {
System.out.println("book dao save......"+num);
}
}
配置文件中配置 bean
<bean id="bookDao" class="com.my.dao.impl.BookDaoImpl">
<constructor-arg name="num" value="10"/>
bean>
测试运行结果
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationConfig.xml");
BookDao bookDao = (BookDao) context.getBean("bookDao");
bookDao.save();
//输出结果为:book dao save......10
}
}
在实现类中,添加构造方法
public class BookServiceImpl implements BookService {
//注入引用类型
private BookDao bookDao;
public BookServiceImpl(BookDao bookDao, UserDao userDao) {
this.bookDao = bookDao;
}
@Override
public void save() {
System.out.println("book service save......");
bookDao.save();
}
}
配置文件中配置 bean
<bean id="bookDao" class="com.my.dao.impl.BookDaoImpl">
<constructor-arg name="connectionNum" value="10"/>
bean>
<bean id="bookService" class="com.my.service.impl.BookServiceImpl">
<constructor-arg name="bookDao" ref="bookDao"/>
bean>
测试运行结果
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationConfig.xml");
BookService bookService = (BookService) context.getBean("bookService");
bookService.save();
//输出结果为:book service save......
//book dao save......10
}
创建一个实现类,添加集合:数组、List、Set、Map、Properties 。 并提供 setter 方法
public class BookDaoImpl implements BookDao {
private int[] array;
private List<String> list;
private Set<String> set;
private Map<String,String> map;
private Properties properties;
public void setArray(int[] array) {
this.array = array;
}
public void setList(List<String> list) {
this.list = list;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public void save() {
System.out.println("array数组:"+ Arrays.toString(array));
System.out.println("list数组:"+list);
System.out.println("set数组:"+set);
System.out.println("map数组:"+map);
System.out.println("properties数组:"+properties);
}
}
配置文件,注入集合类型数据
<bean id="bookDao" class="com.my.dao.impl.BookDaoImpl">
<property name="array">
<array>
<value>1value>
<value>2value>
<value>3value>
array>
property>
<property name="list">
<list>
<value>avalue>
<value>bvalue>
<value>cvalue>
list>
property>
<property name="set">
<set>
<value>avalue>
<value>bvalue>
<value>cvalue>
<value>cvalue>
set>
property>
<property name="map">
<map>
<entry key="1" value="a"/>
<entry key="2" value="b"/>
<entry key="3" value="c"/>
map>
property>
<property name="properties">
<props>
<prop key="4">dprop>
<prop key="5">eprop>
<prop key="6">fprop>
props>
property>
bean>
测试运行结果
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationConfig.xml");
BookDao bookDao = (BookDao) context.getBean("bookDao");
bookDao.save();
//输出结果为:array数组:[1, 2, 3]
//list数组:[a, b, c]
//set数组:[a, b, c]
//map数组:{1=a, 2=b, 3=c}
//properties数组:{4=d, 5=e, 6=f}
}
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.22.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.11version>
dependency>
<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
">
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
bean>
测试运行结果
public static void main(String[] args) {
ApplicationContext context =new ClassPathXmlApplicationContext("applicationConfig.xml");
DataSource dataSource = (DataSource) context.getBean("dataSource");
System.out.println(dataSource);
//输出结果为:{
// CreateTime:"2022-09-18 19:45:03",
// ActiveCount:0,
// PoolingCount:0,
// CreateCount:0,
// DestroyCount:0,
// CloseCount:0,
// ConnectCount:0,
// Connections:[
// ]
//}
}
类路径加载配置文件
ApplicationContext context =new ClassPathXmlApplicationContext("applicationConfig.xml");
文件路径加载配置文件
ApplicationContext context1=new FileSystemXmlApplicationContext("D:\\SpringProjects\\spring_07_dataSource\\src\\main\\resources\\applicationConfig.xml");
加载多个配置文件,用逗号隔开
ApplicationContext context =new ClassPathXmlApplicationContext("applicationConfig.xml","applicationConfig2.xml");
使用 bean 名称获取
BookDao bookDao = (BookDao) context.getBean("bookDao");
使用 bean 名称获取并指定类型
BookDao bookDao = context.getBean("bookDao", BookDao.class);
使用 bean 类型获取
BookDao bookDao = context.getBean(BookDao.class);