property
标签ref
属性注入引用类型对象
<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="bookDao" class="com.practice.dao.impl.BookDaoImpl1"/>
<bean id="userDao" class="com.practice.dao.impl.UserDaoImpl"/>
<bean id="bookService" class="com.practice.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"/>
<property name="userDao" ref="userDao"/>
bean>
beans>
set
方法package com.practice.service.impl;
import com.practice.dao.BookDao;
import com.practice.dao.UserDao;
import com.practice.dao.impl.BookDaoImpl1;
import com.practice.service.BookService;
/**
* @Author YJ
* @Date 2023/7/25 11:28
* Description:
*/
public class BookServiceImpl implements BookService{
private BookDao bookDao;
private UserDao userDao;
public void setBookDao(BookDaoImpl1 bookDao) {
this.bookDao=bookDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
System.out.println("bookService save...");
bookDao.save();
userDao.save();
}
}
package com.practice;
import com.practice.dao.BookDao;
import com.practice.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author YJ
* @Date 2023/7/26 11:32
* Description:
*/
public class AppForDiSet {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = (BookService) act.getBean("bookService");
bookService.save();
}
}
property
标签value
属性注入简单数据类型
<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="bookDao" class="com.practice.dao.impl.BookDaoImpl1">
<property name="databaseName" value="mysql"/>
<property name="connectionNum" value="5"/>
bean>
<bean id="userDao" class="com.practice.dao.impl.UserDaoImpl"/>
<bean id="bookService" class="com.practice.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"/>
<property name="userDao" ref="userDao"/>
bean>
beans>
set
方法package com.practice.dao.impl;
import com.practice.dao.BookDao;
/**
* @Author YJ
* @Date 2023/7/26 11:24
* Description:
*/
public class BookDaoImpl1 implements BookDao {
private int connectionNum;
private String databaseName;
public void setConnectionNum(int connectionNum) {
this.connectionNum = connectionNum;
}
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
public void save() {
System.out.println("bookDao save~" + databaseName + "," + connectionNum);
}
}
package com.practice;
import com.practice.dao.BookDao;
import com.practice.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author YJ
* @Date 2023/7/26 11:40
* Description:
*/
public class AppForDiSet {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = (BookService) act.getBean("bookService");
bookService.save();
}
}
<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="bookDao" class="com.practice.dao.impl.BookDaoImpl1"/>
<bean id="userDao" class="com.practice.dao.impl.UserDaoImpl"/>
<bean id="bookService" class="com.practice.service.impl.BookServiceImpl">
<constructor-arg name="bookDao" ref="bookDao"/>
<constructor-arg name="userDao" ref="userDao"/>
bean>
beans>
package com.practice.service.impl;
import com.practice.dao.BookDao;
import com.practice.dao.UserDao;
import com.practice.dao.impl.BookDaoImpl1;
import com.practice.service.BookService;
/**
* @Author YJ
* @Date 2023/7/25 11:28
* Description:
*/
public class BookServiceImpl implements BookService{
private BookDao bookDao;
private UserDao userDao;
/**
* 构造方法
* @param bookDao
* @param userDao
*/
public BookServiceImpl(BookDao bookDao, UserDao userDao) {
this.bookDao = bookDao;
this.userDao = userDao;
}
public void save() {
System.out.println("bookService save...");
bookDao.save();
userDao.save();
}
}
public class AppForDiSet {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = (BookService) act.getBean("bookService");
bookService.save();
}
}
<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="bookDao" class="com.practice.dao.impl.BookDaoImpl1">
<constructor-arg name="databaseName" value="mysql"/>
<constructor-arg name="connectionNum" value="56"/>
bean>
<bean id="userDao" class="com.practice.dao.impl.UserDaoImpl"/>
<bean id="bookService" class="com.practice.service.impl.BookServiceImpl">
<constructor-arg name="bookDao" ref="bookDao"/>
<constructor-arg name="userDao" ref="userDao"/>
bean>
beans>
package com.practice.dao.impl;
import com.practice.dao.BookDao;
/**
* @Author YJ
* @Date 2023/7/25 11:24
* Description:
*/
public class BookDaoImpl1 implements BookDao {
private int connectionNum;
private String databaseName;
/**
* 构造方法
* @param connectionNum
* @param databaseName
*/
public BookDaoImpl1(int connectionNum, String databaseName) {
this.connectionNum = connectionNum;
this.databaseName = databaseName;
}
public void save() {
System.out.println("bookDao save~" + databaseName + ", " + connectionNum);
}
}
public class AppForDiSet {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = (BookService) act.getBean("bookService");
bookService.save();
}
}
<bean id="bookDao" class="com.practice.dao.impl.BookDaoImpl1">
<constructor-arg type="java.lang.String" value="mysql"/>
<constructor-arg type="int" value="56"/>
bean>
<bean id="userDao" class="com.practice.dao.impl.UserDaoImpl"/>
<bean id="bookService" class="com.practice.service.impl.BookServiceImpl">
<constructor-arg name="bookDao" ref="bookDao"/>
<constructor-arg name="userDao" ref="userDao"/>
bean>
<bean id="bookDao" class="com.practice.dao.impl.BookDaoImpl1">
<constructor-arg index="1" value="mysql"/>
<constructor-arg index="0" value="516"/>
bean>
<bean id="userDao" class="com.practice.dao.impl.UserDaoImpl"/>
<bean id="bookService" class="com.practice.service.impl.BookServiceImpl">
<constructor-arg name="bookDao" ref="bookDao"/>
<constructor-arg name="userDao" ref="userDao"/>
bean>
<bean id="bookDao" class="com.practice.dao.impl.BookDaoImpl2"/>
<bean id="bookService" class="com.practice.service.impl.BookServiceImpl1" autowire="byType"/>
public class BookDaoImpl2 implements BookDao {
public void save() {
System.out.println("bookDao save~" );
}
}
public class BookServiceImpl1 implements BookService {
private BookDao bookDao;
public void save() {
System.out.println("bookService save...");
bookDao.save();
}
public void setBookDao(BookDaoImpl2 bookDao) {
this.bookDao = bookDao;
}
}
public class AppForDiSet {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = (BookService) act.getBean("bookService");
bookService.save();
}
}
<bean id="bookDao" class="com.practice.dao.impl.BookDaoImpl2"/>
<bean id="bookService" class="com.practice.service.impl.BookServiceImpl1" autowire="byName"/>
public class BookServiceImpl1 implements BookService {
private BookDao bookDao;
public void save() {
System.out.println("bookService save...");
bookDao.save();
}
public void setBookDao(BookDaoImpl2 bookDao) {
this.bookDao = bookDao;
}
}
通过这篇文章我们掌握了几个比较方便的注入方式,这在之后的注解开发会经常用到,欢迎各位点赞+关注!!!