Spring——容器,注入(配置和注解两种方式)

一、Spring的容器:

1.什么是控制反转:传统的方法,当某个java对象A需要调用对象B时,是由调用者(对象A)通过new关键字来创建对象B的,而在Spring中,则是由spring完成创建的,所以“控制反转”了。

2.容器:

Spring容器是Spring的核心。

实例化一个spring容器,容器会自动预初始化所有Bean实例。

[java]  view plain  copy
  1. ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //实例化一个Spring容器,  
  2. Boss boss = (Boss) ac.getBean("boss"); //取得对象boss  

二、装配(注入):

1.设置注入:属性必须写setter方法格式为:

<bean id=" " class=" ">

    <property name=" " value=" " />

    <property name=" " value=" " />

</bean>

 

[java]  view plain  copy
  1. <bean id="boss" class="wang.spring.Boss">  
  2.     <property name="car" ref="car"/>  
  3.     <property name="office" ref="office" />  
  4. </bean>  
  5. <bean id="office" class="wang.spring.Office">  
  6.     <property name="officeNo" value="002"/>  
  7. </bean>  
  8. <bean id="car" class="wang.spring.Car" scope="singleton">  
  9.     <property name="brand" value=" 红旗 CA72"/>  
  10.     <property name="price" value="2000"/>  
  11. </bean>  

2.构造注入:必须写有参的构造函数。

<bean id="car" class="wang.spring.Car ">

  <constructor-arg  index=" 0" type=“int” value=" 001” />

  <constructor-arg  index=" 1" type=“String” value=" baoma” />

</bean>

利用上面代码,可以新建一个实例car,并给它的属性赋值。

3.依赖注入:当引用了其他类的对象时。

<property name=" " ref=" ">

或者

<constructor-arg ref=" ">

[java]  view plain  copy
  1. <beans>    
  2.        <bean id=”bean1” class=”……”>    
  3.               <property name=”属性1” value=”……”/>    
  4.        </bean>    
  5. <bean id=”bean2” class=”……”>    
  6.               <property name=”bean1” ref=”bean1”/>    
  7.        </bean>    
  8. </beans>    

4.集合的注入:

Set集合注入:

[java]  view plain  copy
  1. <span style="color:#000000;"><bean id=”……” class=”……”>    
  2. <set>    
  3.               <value>value1</value>    
  4.               <value>value2</value>    
  5.               ……    
  6.        </set>    
  7. </bean>  </span>  

Lsit集合注入:

[java]  view plain  copy
  1. <span style="color:#000000;"><bean id=”……” class=”……”>    
  2.    <list>    
  3.         <value>value1</value>    
  4.         <value>value2</value>    
  5.               ……    
  6.     </list>    
  7. </bean> </span>  

Map集合注入:

[java]  view plain  copy
  1. <span style="color:#000000;"><bean id=”……” class=”……”>    
  2. <map>    
  3.               <entry key=”key1” value=”value1”>    
  4.               <entry key=”key2” value=”value2”>    
  5.               ……    
  6.        </map>    
  7. </bean> </span>  

Properties注入

[java]  view plain  copy
  1. <span style="color:#000000;"><bean id=”……” class=”……”>  
  2. <props>  
  3.               <prop key=”key1”>value1</prop>  
  4.               <prop key=”key2”>value2</prop>  
  5.               ……  
  6.        </props>  
  7. </bean></span>  


5.自动注入:

通过bean里的属性autowire可以自动注入实例。

no:不使用自动装配。这是默认配置。
byName:根据属性自动装配,BeanFactory会查找容器中所有的Bean,找出id属性与属性名同名的Bean来完成注入。如果没有找到匹配的Bean,Spring则不会进行任何注入。
byType:根据属性类型自动装配,BeanFactroy会查找容器中所有的 Bean,如果一个正好与依赖属性类型相同的Bean,就会自动注入这个属性。
如果有多个这样的Bean则会抛出异常。如果没有这样 的Bean则什么也不会发生,属性不会被设置。
[java]  view plain  copy
  1. <bean class="dao.OrderDaoImpl" autowire="byName"></bean>  
  2. <bean class="dao.ItemDaoImpl"></bean>  
  3. <bean id="storeService" class="service.StoreServiceImpl"></bean>  
  4.   
  5. lt;/beans>  

三、通过注解自动装配:

注解可以分为两大类。JSR-250规范注解方式 和 Spring自带的注解方式。

通过注解的方式装配时,必须在配置文件中添加一个bean,它其实是一个注解处理器,用于解析注解。

JSR-250规范注解方式的处理器:

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

Spring自带的注解方式的处理器:

<bean class="org.sprinframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor"/>

当然,我们有更方便的方法,两种注解方式都可以用下面的方式隐式得注册注解处理器:

  <context:annotation-config>  

 

1.JSR-250规范注解方式:

@Resource

默认是按照名称匹配(byName),但它有两个属性,name和type。分别可以指定自动匹配的类型。

标注在成员变量,setter方法,构造函数上都可以。

[java]  view plain  copy
  1. public class Boss {  
  2.     // 自动注入类型为 Car 的 Bean  
  3.     @Resource  
  4.     private Car car;  
  5.   
  6.     // 自动注入 bean 名称为 office 的 Bean  
  7.     @Resource(name = "office")  
  8.     private Office office;  
  9. }  
  10.    

2.Spring自带的注解方式:

@Autowired

按照类型匹配(byType)

标注在成员变量,setter方法,构造函数上都可以。

因为这种注解是按照类型注入的,所以要有多个同类型的对象,则会出现异常。此时我们可以用@Qualifier指定对象的名称,两者结合使用,便可以确定一个对象。

[java]  view plain  copy
  1. public class Boss {  
[java]  view plain  copy
  1.    
  2.     @Autowired  
  3.     @Qualifier("office")  
  4.     private Office office;  
  5.     //<span style="color:#006600;">当在成员变量上标注时,不需要写setter方法…  
  6. </span>}  
  7.    
  8. @Qualifier的标注对象是成员变量、方法入参、构造函数入参。正是由于注释对象的不同,所以 Spring 不将 <code>@Autowired</code> 和<code>@Qualifier</code> 统一成一个注释类。(@Qualifier只能结合@<strong>Autowired</strong>使用,不能单独使用)  


四、实现零配置:

我们可以通过上面两种注解方式,在bean中实现自动注入。但是bean还得在xml文件中通过<bean>进行定义。那我们能不能也通过注解方式定义bean,实现零配置呢。答案是肯定的。

Spring提供如下几个Annotation来标注Spring Bean:
@Component标注一个普通的Spring Bean;
@Controller:标注一个控制器组件类;
@Service:标注一个业务逻辑组件类;
@Repository:标注一个Dao组件;
目前Component可在各层通用。
[java]  view plain  copy
  1. @Component  
  2. public class Boss {  
  3.     @Autowired  
  4.     private Car car;  
  5.   
  6.     @Autowired  
  7.     private Office office;  
  8.     …  
  9. }  

这时,bean.xml文件中就不必用<bean>来定义bean了。只需添加一行<context:component-scan base-package="com.baobaotao"/>用来指定这些类所在的包。  
<context:component-scan base-package="com.baobaotao"/>

posted @   silentmuh  阅读(226)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
Live2D
欢迎阅读『Spring——容器,注入(配置和注解两种方式)』
  1. 1 Walk Thru Fire Vicetone
  2. 2 爱你 王心凌
  3. 3 Inspire Capo Productions - Serenity
  4. 4 Welcome Home Radical Face
  5. 5 粉红色的回忆 李玲玉
Walk Thru Fire - Vicetone
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Van Der Voort, Joren Johannes

作曲 : Victor Pool/Justin Gammella/Ruben Christopher den Boer/Meron Mengist/Joren van der Voort

Talk to me

Spill the secrets you've been keeping

Life cuts deep

Let me help pick up the pieces

You're not alone, I'm by your side

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

No matter what, I'll make it right

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

I'm not an angel, I'm not a saint

I've been a closed book full of mistakes

But when you're broken, when you're in pain

Oooh, ooh

I'll walk through fire with you

I'll walk through fire

I'll walk through fire with you

I'll walk through fire

You know I

Don't pretend to be a savior

But let me in, yeah

I promise nobody can break us

You're not alone, I'm by your side

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

No matter what, I'll make it right

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

I'm not an angel, I'm not a saint

I've been a closed book full of mistakes

But when you're broken, when you're in pain

Oooh, ooh

I'll walk through fire with you

I'll walk through fire with you

I'll walk through fire

I'll walk through fire with you

I'll walk through fire with you

You're not alone, I'm by your side

Don't you know, don't you know

I'll walk through fire with you

I'll walk through fire

I'm not an angel, I'm not a saint

I've been a closed book full of mistakes

But when you're broken, when you're in pain

Oooh, ooh

I'll walk through fire with you

I'll walk through fire with you

I'll walk through fire

I'll walk through fire with you

I'll walk through fire with you

点击右上角即可分享
微信分享提示