Spring study notes 02

9. The Spring dependency injection

  1. Based Setter injection method
  2. Constructor method based on injection
  3. Automated assembly
  • Based Setter injection method

The basic properties of injection

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

Or write in the property tag

<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-namespace

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

Reference properties injection

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

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

    </bean>

or

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

Bean internal use

<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>

A collection of attributes injection

@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=高})
  • Builder input
<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>
  • Automatic assembly 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. the Spring annotation configuration

<?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>

Determining the scanning range Spring assembly, must pay attention, not within the scope of the Bean will be injected into the vessel IOC, the need to add such Component corresponding comments on the class.

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

Spring common annotation
@Component : refers assembly
@Service: refers to the business layer components, normally used in XXXServiceImpl
@Repository: refers to the persistence layer assembly, typically used in XXXDaoImpl
@Controller : refers to the control layer assembly.
The three notes can be configured bean names, of course, also be the default
@Component [( "beanName")] , is used by default if the default class name first letter lowercase. For example UserDaoImpl name defaults to UserDAOImpl
@Value : for the property assignment notes
the @Scope : Configuring scope of the bean (Singleton | prototype)
@Autowired : automatically inject dependencies byType @Quali fi er ( "beanName"@Autowired and implement joint use of injection byName
@Resource (name = "the beanName") : equivalent to @Autowired @Quali fi er and used in combination, but not the annotation belongs Spring J2EE specification.

11. ** Spring integration 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();   
    }
    
 }

Guess you like

Origin blog.csdn.net/dingcai12003/article/details/90708400