<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.21.RELEASEversion>
dependency>
package com.practice.dao;
/**
* @Author YJ
* @Date 2023/7/25 11:26
* Description:
*/
public interface BookDao {
void save();
}
package com.practice.service;
/**
* @Author YJ
* @Date 2023/7/25 11:28
* Description:
*/
public interface BookService {
void save();
}
package com.practice.service.impl;
import com.practice.dao.BookDao;
import com.practice.dao.impl.BookDaoImpl;
import com.practice.service.BookService;
/**
* @Author YJ
* @Date 2023/7/25 11:28
* Description:
*/
public class BookServiceImpl implements BookService {
private BookDao bookDao = new BookDaoImpl();
public void save() {
bookDao.save();
}
}
applicationContext.xml
,配置对应类作为Spring管理的bean
<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.BookDaoImpl"/>
<bean id="bookService" class="com.practice.service.impl.BookServiceImpl"/>
beans>
public class App {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean
/*BookDao bookDao = (BookDao) act.getBean("bookDao");
bookDao.save();*/
BookService bookService = (BookService) act.getBean("bookService");
bookService.save();
}
}
new
的方式创建的dao对象setter
方法service
与dao的关系property
标签表示配置当前bean的属性name
属性表示配置哪一个具体的属性ref
属性表示参照哪一个beanpackage com.practice.service.impl;
import com.practice.dao.BookDao;
import com.practice.service.BookService;
public class BookServiceImpl implements BookService {
//删除业务层中使用new的方式创建的dao对象
private BookDao bookDao;
public void save() {
bookDao.save();
}
//提供对应的set方法
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
}
<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.BookDaoImpl"/>
<bean id="bookService" class="com.practice.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"/>
bean>
beans>
<beans>
<bean/>
<bean>bean>
beans>
<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.BookDaoImpl"/>
<bean id="bookService" name="service" class="com.practice.service.impl.BookServiceImpl">
<property name="bookDao" ref="bookDao"/>
bean>
beans>
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/25 11:33
* Description:
*/
public class App {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean
/*BookDao bookDao = (BookDao) act.getBean("bookDao");
bookDao.save();*/
BookService bookService = (BookService) act.getBean("service");//用到别名service
bookService.save();
}
}
NoSuchBeanDefinitionException
异常,表示该bean未定义public class App {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean
/*BookDao bookDao = (BookDao) act.getBean("bookDao");
bookDao.save();*/
BookService bookService = (BookService) act.getBean("service1");//用到错误别名service1
bookService.save();
}
}
public class AppForScope {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
BookDao bookDao1 = (BookDao) act.getBean("bookDao");
BookDao bookDao2 = (BookDao) act.getBean("bookDao");
System.out.println(bookDao1);
System.out.println(bookDao2);
}
}
scope
属性定义bean的作用范围,可选范围如下:singleton
:单例(默认)prototype
:非单例public class BookDaoImpl implements BookDao {
private BookDaoImpl() {
System.out.println("book dao constructor running...");
}
public void save() {
System.out.println("bookDao save~");
}
}
public class App {
public static void main(String[] args) {
//获取IoC容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean
BookDao bookDao = (BookDao) act.getBean("bookDao");
bookDao.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.BookDaoImpl" scope="prototype"/>
<bean id="bookService" name="service" class="com.practice.service.impl.BookServiceImpl" >
<property name="bookDao" ref="bookDao"/>
bean>
beans>
BeanCreationException
异常
<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="userFactory" class="com.practice.factory.UserDaoFactory"/>
<bean id="userDao" factory-bean="userFactory" factory-method="getUserDao"/>
beans>
public interface UserDao {
public void save();
}
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("UserDao save...");
}
}
public class UserDaoFactory {
public UserDao getUserDao(){
return new UserDaoImpl();
}
}
package com.practice;
import com.practice.dao.UserDao;
import com.practice.factory.UserDaoFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author YJ
* @Date 2023/7/25 19:09
* Description:
*/
public class AppForInstanceUser {
public static void main(String[] args) {
/*//创建实例工厂对象
UserDaoFactory userDaoFactory = new UserDaoFactory();
UserDao userDao = userDaoFactory.getUserDao();
userDao.save();*/
//获取IoC容器
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean
UserDao userDao = (UserDao) act.getBean("userDao");
userDao.save();
}
}
package com.practice.factory;
import com.practice.dao.UserDao;
import com.practice.dao.impl.UserDaoImpl;
import org.springframework.beans.factory.FactoryBean;
/**
* @Author YJ
* @Date 2023/7/25 19:21
* Description:使用FactoryBean实例化bean
*/
public class UserDaoFactoryBean implements FactoryBean<UserDao> {
public UserDao getObject() throws Exception {
//代替原始实例工厂中创建对象的方法
return new UserDaoImpl();
}
public Class<?> getObjectType() {
return UserDao.class;
}
}
<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="userFactory" class="com.practice.factory.UserDaoFactory"/>
<bean id="userDao1" factory-bean="userFactory" factory-method="getUserDao"/>
<bean id="userDao" class="com.practice.factory.UserDaoFactoryBean"/>
beans>
public boolean isSingleton() {
return false;
}
package com.practice.dao.impl;
import com.practice.dao.BookDao;
/**
* @Author YJ
* @Date 2023/7/25 11:24
* Description:
*/
public class BookDaoImpl1 implements BookDao {
public void save() {
System.out.println("bookDao save~");
}
//表示bean初始化
public void init() {
System.out.println("init...");
}
//表示bean销毁前对应操作
public void destroy() {
System.out.println("destroy...");
}
}
<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" init-method="init" destroy-method="destroy"/>
beans>
package com.practice;
import com.practice.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author YJ
* @Date 2023/7/25 19:40
* Description:
*/
public class AppForLifeCycle {
public static void main(String[] args) {
//获取IoC容器
ClassPathXmlApplicationContextact = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean
BookDao bookDao = (BookDao) act.getBean("bookDao");
bookDao.save();
//关闭容器,才能看到destory方法的执行结果
//1.暴力关闭
act.close();
//2.关闭钩子
act.registerShutdownHook();
}
}
package com.practice.service.impl;
import com.practice.dao.BookDao;
import com.practice.dao.impl.BookDaoImpl1;
import com.practice.service.BookService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
/**
* @Author YJ
* @Date 2023/7/25 11:28
* Description:
*/
public class BookServiceImpl1 implements BookService, InitializingBean, DisposableBean {
private BookDao bookDao;
public void save() {
System.out.println("bookService save...");
bookDao.save();
}
public void afterPropertiesSet() throws Exception {
System.out.println("service init...");
}
public void destroy() throws Exception {
System.out.println("service destroy...");
}
public void setBookDao(BookDaoImpl1 bookDao) {
this.bookDao=bookDao;
}
}
package com.practice;
import com.practice.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @Author YJ
* @Date 2023/7/25 19:40
* Description:
*/
public class AppForLifeCycle {
public static void main(String[] args) {
//获取IoC容器
ClassPathXmlApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取bean
BookDao bookDao = (BookDao) act.getBean("bookDao");
bookDao.save();
//act.close();
act.registerShutdownHook();
}
}
<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" init-method="init" destroy-method="destroy"/>
<bean id="bookService" class="com.practice.service.impl.BookServiceImpl1">
<property name="bookDao" ref="bookDao"/>
bean>
beans>
关于Spring的初步认识我们就学到这里了,关于bean的操作我们有了基本的认识,欢迎各位小伙伴点赞+关注!!!