Spring学习笔记

Spring学习笔记

Bean Definition Inheritance

<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
  <property name="message1" value="Hello World!"/>
  <property name="message2" value="Hello Second World!"/>
</bean>

<bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="helloWorld">
  <property name="message1" value="Hello India!"/>
  <property name="message3" value="Namaste India!"/>
</bean>

Dependency Injection

Constructor-based Dependency Injection

public TextEditor(SpellChecker spellChecker) {
  System.out.println("Inside TextEditor constructor." );
  this.spellChecker = spellChecker;
}
...
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
  <constructor-arg ref="spellChecker"/>
</bean>

<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>

Setter-based Dependency Injection

// a setter method to inject the dependency.
public void setSpellChecker(SpellChecker spellChecker) {
  System.out.println("Inside setSpellChecker." );
  this.spellChecker = spellChecker;
}
...
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
  <property name="spellChecker" ref="spellChecker"/>
</bean>

<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>

Injecting Inner Beans

<!-- Definition for textEditor bean using inner bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
  <property name="spellChecker">
     <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"/>
   </property>
</bean>

Injecting Collection

元素描述
<list>This helps in wiring ie injecting a list of values, allowing duplicates.
<set>This helps in wiring a set of values but without any duplicates.
<map>This can be used to inject a collection of name-value pairs where name and value can be of any type.
<props>This can be used to inject a collection of name-value pairs where the name and value are both Strings.
<!-- Definition for javaCollection -->
<bean id="javaCollection" class="com.tutorialspoint.JavaCollection">

  <!-- results in a setAddressList(java.util.List) call -->
  <property name="addressList">
     <list>
        <value>INDIA</value>
        <value>Pakistan</value>
        <value>USA</value>
        <value>USA</value>
     </list>
  </property>

  <!-- results in a setAddressSet(java.util.Set) call -->
  <property name="addressSet">
     <set>
        <value>INDIA</value>
        <value>Pakistan</value>
        <value>USA</value>
        <value>USA</value>
    </set>
  </property>

  <!-- results in a setAddressMap(java.util.Map) call -->
  <property name="addressMap">
     <map>
        <entry key="1" value="INDIA"/>
        <entry key="2" value="Pakistan"/>
        <entry key="3" value="USA"/>
        <entry key="4" value="USA"/>
     </map>
  </property>

  <!-- results in a setAddressProp(java.util.Properties) call -->
  <property name="addressProp">
     <props>
        <prop key="one">INDIA</prop>
        <prop key="two">Pakistan</prop>
        <prop key="three">USA</prop>
        <prop key="four">USA</prop>
     </props>
  </property>

</bean>

Injecting Bean References

<!-- Bean Definition to handle references and values -->
<bean id="..." class="...">

  <!-- Passing bean reference  for java.util.List -->
  <property name="addressList">
     <list>
        <ref bean="address1"/>
        <ref bean="address2"/>
        <value>Pakistan</value>
     </list>
  </property>

  <!-- Passing bean reference  for java.util.Set -->
  <property name="addressSet">
     <set>
        <ref bean="address1"/>
        <ref bean="address2"/>
        <value>Pakistan</value>
     </set>
  </property>

  <!-- Passing bean reference  for java.util.Map -->
  <property name="addressMap">
     <map>
        <entry key="one" value="INDIA"/>
        <entry key ="two" value-ref="address1"/>
        <entry key ="three" value-ref="address2"/>
     </map>
  </property>

</bean>

Injecting null and empty string values

Injecting null:

<bean id="..." class="exampleBean">
   <property name="email"><null/></property>
</bean>

Injecting empty string values:

<bean id="..." class="exampleBean">
   <property name="email" value=""/>
</bean>

Beans Auto-Wiring

no

This is default setting which means no autowiring and you should use explicit bean reference for wiring. You have nothing to do special for this wiring. This is what you already have seen in Dependency Injection chapter.

byName

Normal condition:

<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
  <property name="spellChecker" ref="spellChecker" />
  <property name="name" value="Generic Text Editor" />
</bean>

<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>

Use autowiring ‘byName’:

<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor" 
  autowire="byName">
  <property name="name" value="Generic Text Editor" />
</bean>

<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>

byType

Normal condition:

<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
  <property name="spellChecker" ref="spellChecker" />
  <property name="name" value="Generic Text Editor" />
</bean>

<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>

Use autowiring ‘byType’:

<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor" 
  autowire="byType">
  <property name="name" value="Generic Text Editor" />
</bean>

<!-- Definition for spellChecker bean -->
<bean class="com.tutorialspoint.SpellChecker">
</bean>

by constructor

Normal condition:

<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
  <constructor-arg  ref="spellChecker" />
  <constructor-arg  value="Generic Text Editor"/>
</bean>

<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>

Use autowiring ‘by constructor’:

<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor" 
  autowire="constructor">
  <constructor-arg value="Generic Text Editor"/>
</bean>

<!-- Definition for spellChecker bean -->
<bean class="com.tutorialspoint.SpellChecker">
</bean>

autodetect

Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType.

autowiring的使用限制

限制描述
可能被覆盖在使用autowiring的同时又使用了<constructor-arg> 和 <property>
原始数据类型autowiring不能使用在原始数据类型上,如String等
容易混淆autowiring没有显示wiring那么明确,应尽量使用显示wiring

Annotation Based Configuration

@Required

@Required应用于bean属性的setter方法。它表明受影响的bean属性必须包含在XML配置文件中,否则会抛出BeanInitializationException异常。

<?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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>

   <!-- Definition for student bean -->
   <bean id="student" class="com.tutorialspoint.Student">
      <property name="name"  value="Zara" />
      <property name="age"  value="11"/>
   </bean>

</beans>

若将属性age去掉,即<!-- property name="age" value="11"-->,则会抛出Property 'age' is required for bean 'student'

@Autowired

@Autowired注释对在哪里以及如何完成自动连接提供了更多的精细的控制。

Setter方法中的@Autowired

可以在Setter方法上使用@Autowired以替代XML配置文件中的<property>元素。当Spring发现在Setter方法上使用了@Autowired,它将在该方法上执行byType自动连接。
TextEditor.java

package com.tutorialspoint;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
   private SpellChecker spellChecker;

   @Autowired
   public void setSpellChecker( SpellChecker spellChecker ){
      this.spellChecker = spellChecker;
   }
   public SpellChecker getSpellChecker( ) {
      return spellChecker;
   }
   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}

Beans.xml

<context:annotation-config/>

<!-- Definition for textEditor bean without constructor-arg  -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
</bean>

<!-- Definition for spellChecker bean -->
<bean class="com.tutorialspoint.SpellChecker">
</bean>

属性中的@Autowired

可以在属性上使用@Autowired以替代Setter方法。
TextEditor.java

package com.tutorialspoint;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
   @Autowired
   private SpellChecker spellChecker;

   public TextEditor() {
      System.out.println("Inside TextEditor constructor." );
   }

   public SpellChecker getSpellChecker( ){
      return spellChecker;
   }

   public void spellCheck(){
      spellChecker.checkSpelling();
   }
}

Beans.xml

<context:annotation-config/>

<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
</bean>

<!-- Definition for spellChecker bean -->
<bean class="com.tutorialspoint.SpellChecker">
</bean>

构造器中的@Autowired

可以在构造器上使用@Autowired以替代<constructor-arg>元素。
TextEditor.java

package com.tutorialspoint;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
   private SpellChecker spellChecker;

   @Autowired
   public TextEditor(SpellChecker spellChecker){
      System.out.println("Inside TextEditor constructor." );
      this.spellChecker = spellChecker;
   }

   public void spellCheck(){
      spellChecker.checkSpelling();
   }
}

Beans.xml

<context:annotation-config/>

<!-- Definition for textEditor bean without constructor-arg  -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
</bean>

<!-- Definition for spellChecker bean -->
<bean class="com.tutorialspoint.SpellChecker">
</bean>

@Autowired的(required=false)选项

默认情况下,@Autowired注解意味着依赖是必须的,即它所依赖的属性必须包含在XML配置文件中。然而,可以通过@Autowired(required=false)选项关闭该行为。同@Required注解类似。
Student.java

package com.tutorialspoint;

import org.springframework.beans.factory.annotation.Autowired;

public class Student {
   private Integer age;
   private String name;

   @Autowired(required=false)
   public void setAge(Integer age) {
      this.age = age;
   }

   public Integer getAge() {
      return age;
   }

   @Autowired
   public void setName(String name) {
      this.name = name;
   }

   public String getName() {
      return name;
   }
}

说明:age属性不需任何传递参数。

@Qualifier

存在这样一种情形:当创建多个同一类型的bean时,在依赖时又只想依赖其中的某一个时该怎么做呢?
我们可以将@Qualifier注解和@Autowired注解一起使用来确定到底依赖哪一个bean。
Profile.java

package com.tutorialspoint;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Profile {
   @Autowired
   @Qualifier("student1")
   private Student student;

   public Profile(){
      System.out.println("Inside Profile constructor." );
   }

   public void printAge() {
      System.out.println("Age : " + student.getAge() );
   }

   public void printName() {
      System.out.println("Name : " + student.getName() );
   }
}

Beans.xml

<context:annotation-config/>

<!-- Definition for profile bean -->
<bean id="profile" class="com.tutorialspoint.Profile">
</bean>

<!-- Definition for student1 bean -->
<bean id="student1" class="com.tutorialspoint.Student">
  <property name="name"  value="Zara" />
  <property name="age"  value="11"/>
</bean>

<!-- Definition for student2 bean -->
<bean id="student2" class="com.tutorialspoint.Student">
  <property name="name"  value="Nuha" />
  <property name="age"  value="2"/>
</bean>
Spring是一个开源的Java框架,用于构建企业级应用程序。它提供了一种轻量级的、非侵入式的开发方式,通过依赖注入和面向切面编程等特性,简化了Java应用程序的开发过程。 以下是关于Spring学习的一些笔记: 1. IoC(控制反转):Spring通过IoC容器管理对象的创建和依赖关系的注入。通过配置文件或注解,将对象的创建和依赖关系的维护交给Spring容器来管理,降低了组件之间的耦合度。 2. DI(依赖注入):Spring通过依赖注入将对象之间的依赖关系解耦。通过构造函数、Setter方法或注解,将依赖的对象注入到目标对象中,使得对象之间的关系更加灵活和可维护。 3. AOP(面向切面编程):Spring提供了AOP的支持,可以将与业务逻辑无关的横切关注点(如日志、事务管理等)从业务逻辑中分离出来,提高了代码的可重用性和可维护性。 4. MVC(模型-视图-控制器):Spring提供了一个MVC框架,用于构建Web应用程序。通过DispatcherServlet、Controller、ViewResolver等组件,实现了请求的分发和处理,将业务逻辑和视图展示进行了分离。 5. JDBC和ORM支持:Spring提供了对JDBC和ORM框架(如Hibernate、MyBatis)的集成支持,简化了数据库访问的操作,提高了开发效率。 6. 事务管理:Spring提供了对事务的支持,通过声明式事务管理和编程式事务管理,实现了对数据库事务的控制和管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值