Spring_IOC_Bean基于xml、annotation、Java的配置

  • 使用Spring前提,在pom.xml中添加依赖
<dependency>
  <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>5.1.12.RELEASE</version>
</dependency>

xml配置

  • application.xml中配置bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
	<!--...-->
</beans>
  • 创建不需要初始化属性的对象
<!-- 配置bean 每一个bean都会对应java中的一个类
	id - 自定义的bean的名字 要求唯一 命名要求 类名首字母变小写
	class - 类名
	lazy-init true 懒加载,第一次使用对象时,才创建
	scope - 
		singleton 默认单例模式
		默认是ioc容器初始化时就会被创建出来
		prototype 原型模式 
		每次从容器中返回的都是一个对象的新的副本 clone方法创建的对象
		该对象是使用时才会被创建出来
 -->
<bean id="pomainDao" class="com.test.dao.PomainDao"></bean>   
<bean id="poitemDao" class="com.test.dao.PoitemDao"></bean>  
<bean  id="pomainDao1" class="com.test.dao.PomainDao" lazy-init="true"></bean> 
<bean id="productDao" class="com.test.dao.ProductDao" scope="prototype"></bean>   
  • 创建需要初始化属性的对象,并注入依赖
<!-- 
    2.1 创建pomainService对象分为两步
	    先无参数构造方法反射创建对象
	    调用对象的setter方法注入成员属性对象
-->
<bean id="pomainService" class="com.test.service.PomainService">
 		<!--配置属性的依赖关系
 		name 对应类中的set方法后的名字首字母变小写
 		ref 指向ioc容器中的 另外一个bean的名字
 		-->
 	<property name="pomainDao" ref="pomainDao"></property>
 	<property name="poitemDao" ref="poitemDao"></property>
 	<property name="productDao" ref="productDao"></property>
 </bean> 
      
      
<!-- 2.2 调用构造方法完成成员属性的初始化 -->
<bean id="pomainService2" class="com.test.service.PomainService">
	<!--配置构造方法的参数-->
	<constructor-arg name="poitemDao" ref="poitemDao"></constructor-arg>
	<constructor-arg name="pomainDao" ref="pomainDao"></constructor-arg>
	<constructor-arg name="productDao" ref="productDao"></constructor-arg>
</bean>
	  
<!-- 2.3 自动装配属性
  	autowire 自动装配成员属性,默认不会自动装配的
	byName 根据类中属性对应的setter方法,
自动在ioc容器中查找和该set方法后名字首字母变小写后的字符串相同的bean的名字自动装配
	byType 根据类中属性的类型 在ioc容器中查找和该类型相同的bean对象自动装配
  			要求:同一种类型的bean只能有一个,不然会有歧义
-->
<bean id="pomainService3" class="com.test.service.PomainService" autowire="byName"></bean>

xml+annotation配置

  • 在application.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 添加注解扫描路径,路径是基于包的-->
    <context:component-scan base-package="com.test.dao"></context:component-scan>
    <context:component-scan base-package="com.test.service"></context:component-scan>
</beans>
  • 创建注解
    创建对象的注解添加在类上
    等价于在xml文件中配置<bean id="" class=""></bean>
    添加注解后 创建的bean的名字默认为 类名首字母变小写

    @Repository 添加到持久层的类上的
    @Service 添加到service层的类上的
    @Controller 添加到控制层的类上的
    @Component 添加到其他的类上

    依赖注入的
    @Autowired 添加在属性上 自动装配该属性 byName+byType

@Repository 
public class PoitemDao {}
...
@Service
public class PomainService {
	// 属性名命名规范:类名首字母变小写 
	//如果是以接口声明,属性名字可以写实现类类型首字母变小写
	@Autowired
	private PomainDao pomainDao;
	@Autowired
	private PoitemDao poitemDao;
	@Autowired
	private ProductDao productDao;
}

Java+annotation配置

@Configuration添加在一个类上的,
这个类我们叫做spring的ioc启动类 等价于<beans></beans>

@ComponentScan添加在启动类上
等价于<context:component-scan base-package=""></context:component-scan>

@Configuration
@ComponentScan({"com.test.dao","com.test.service"})
public class AppConfig {
	
	/**
	 * @Bean 添加在一个方法上
	 * 会在ioc容器中创建一个对象
	 * 名字和方法名称相同
	 * 对象就是该方法返回的对象
	 * @return
	 */
	@Bean
	public String hello() {
		return "你好";
	}
}

其他注解配置和xml+annotation中的配置相同

  • 获取bean对象
// 初始化IOC容器  通过读取xml的配置文件
// 在初始化时,默认会把单例的bean对应的对象创建出来
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
		
// 1. 从容器中获取对象  
// 1.1 根据bean的名字和类型获取对象
PomainDao pomainDao = applicationContext.getBean("pomainDao", PomainDao.class);
// 1.2 根据bean的名字获取对象 
pomainDao = (PomainDao) applicationContext.getBean("pomainDao");
// 1.3 根据类型获取对象
ProductDao productDao = applicationContext.getBean(ProductDao.class);		
发布了340 篇原创文章 · 获赞 23 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Chill_Lyn/article/details/103755326