Spring学习笔记02

9. spring 依赖注入

  1. 基于Setter方法注入
  2. 基于构造器方法注入
  3. 自动装配
  • 基于Setter方法注入

基本属性注入

<bean name="car" class="com.cd.pojos.Car">
        <property name="name" value="ae86"/>
        <property name="brand" value="宝马"/>
        <property name="price" value="3200000"/>
    </bean>

或者说把属性写在标签里

<bean class="com.cd.pojo.Car" name="car2">  
<property name="brand">    
<value>奥迪</value>   
</property>    
<property name="name">       
<value>A6L</value>  
</property>  
<property name="price">  
<value>340000</value>  
</property> 
</bean>

使用p名称空间

xmlns:p="http://www.springframework.org/schema/p"
<bean class="com.cd.pojos.Car" name="car3" p:name="揽胜" p:brand="路虎" p:price="8008085"/

引用属性注入

  <bean class="com.cd.pojos.Person" name="person">

        <property name="name" value="张三"/>
        <property name="car" ref="car"/>

    </bean>

或者

<bean class="com.cd.pojo.Person" name="person2">    
<property name="username" value="张三"/>   
<property name="car">
<ref bean="car2"/>        
</property>
</bean>

使用内部Bean

<bean class="com.cd.pojos.Person" name="personInner">

        <property name="name" value="李四"/>
        <property name="car">

            <bean class="com.cd.pojos.Car" name="cc">
                <property name="name" value="x5"/>
                <property name="price" value="20200200"/>
                <property name="brand" value="捷豹"/>
            </bean>

        </property>

    </bean>

集合属性注入

@Data
public class foo {

   private String[] arr;
   private List<String> list;
   private Set<Car> sets;
   private Map<String, Object> maps;
   private Properties props;

}
<bean class="com.cd.pojo.Foo" name="foo">   
     <property name="arr">         
      <array value-type="java.lang.String">     
  			 <value>西瓜</value>          
 			 <value>馒头</value>          
  		     <value>苹果</value>     
      </array> 
       </property> 
    <property name="list">      
      <list>              
   		      <value>衣服</value> 
   		      <value>裤子</value>
   		      <value>帽子</value>
    </list>        
    </property>       
     <property name="set">      
     <set>  
            <value>手机</value>
            <value>电脑</value>
            <value>笔记本</value>
</set>      
  </property>
    <property name="map">
   	     <map>  
   		     <entry key="k1" value="java"/>
   		     <entry key="k2" value="world"/> 
   		     <entry key="k3" value-ref="car"/>
   	     </map>
     </property>
    <property name="prop"> 
   	     <props>
   			     <prop key="s1"></prop>
   			     <prop key="s2"></prop>
   			     <prop key="s3"></prop>
   	     </props> 
    </property>
    </bean>
Foo(arr=[西瓜, 馒头, 苹果], list=[衣服, 裤子, 帽子], set=[手机, 电脑, 笔记本], map={k1=java, k2=world, k3=Car(brand=奥迪, name=A6L, price=340000.0)}, prop={s3=帅, s2=富, s1=高})
  • 构造器输入
<bean class="com.cd.pojo.Animal" name="animal">  
		<constructor-arg name="name" value="毛毛"/>
		<constructor-arg name="age" value="20">
		<constructor-arg name="leg" value="2"/> 
</bean>
index 参数索引(从0开始) name 指定参数名称 如果没有指明 name 和 index 会安构造器的参数顺序依次注 入

<bean class="com.cd.pojo.Animal" name="animal">
		<constructor-arg index="0" value="猩猩"/>
		<constructor-arg index="1" value="20"/>
		<constructor-arg index="2" value="4"/> 
</bean>
  • 自动装配 autowire

byName

<bean class="com.cd.pojo.Person" name="person" autowire="byName">  //通过名字匹配ioc容器里的name值注入
 		<property name="username" value="kangkang"/>
</bean>

byType

<bean class="com.cd.pojo.Person" name="person" autowire="byType">   //通过类型匹配ioc容器里的类型注入
		<property name="username" value="kangkang"/> 
</bean>
---------------------------------------------------------------------------------------------------------------------------
<bean class="com.cd.pojo.Car" name="car" autowire-candidate="false"> //通过设置 autowire-candidate是否为候选bean
   	 <property name="brand" value="奥迪"/>
   	 <property name="name" value="A6L"/>
   	 <property name="price" value="340000"/>
</bean> 

constructor

<bean class="com.cd.pojo.Person" name="person" autowire="constructor" >
		<constructor-arg name="username" value="echo"/>
</bean>
//与 byTpye类似,区别在于是通过构造器赋值。

10. spring 注解配置

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> 
     <!-- 配置组件扫描  -->    
   <context:component-scan base-package="com.xx"/>
</beans>

确定Spring 扫描组件的范围,一定注意,不是在范围内的Bean都会注入到IOC容器,需要在类上加上 相应的 注解 比如 Component。

@Component 
@Data 
public class Car {
    @Value("大众")
    private String brand;
    @Value("迈腾")
    private String name;
    @Value("400000")
    private double price; 
 }

Spring 常用注解
@Component : 泛指组件
@Service : 指业务层组件,通常用在 XXXServiceImpl
@Repository : 指持久层组件,通常用在XXXDaoImpl
@Controller : 指控制层组件。
上述三个注解 可以配置bean的名字,当然也可以缺省
@Component[(“beanName”)],如果缺省默认使用类名 首字母小写。比如 UserDaoImpl 名字默认为 userDaoImpl
@Value : 为属性赋值的注解
@Scope:配置bean 的作用域 (singleton | prototype)
@Autowired : 自动注入依赖 byType @Qualifier(“beanName”):和@Autowired 联合使用 实现 byName注入
@Resource(name = “beanName”): 等效于 @Autowired 和 @Qualifier 联合使用,只不过该注解不属于Spring 属于 J2EE规范。

11. **Spring 整合JunitTest **

pox.xml

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</version>
<scope>test</scope>
</dependency>

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("classpath:beans.xml")
public class AppTest {   
 @Autowired    
 UserService userService; 
   
   @Test   
   public void test(){        
   userService.plaseSayHello();   
    }
    
 }

猜你喜欢

转载自blog.csdn.net/dingcai12003/article/details/90708400