1.XMLベースのBeanインジェクション
1.コンストラクター
インジェクション
2.メソッドインジェクションの設定
2.Beanの自動インジェクション[ Spring自動アセンブリ戦略]自動インジェクション[自動アセンブリ] -Springコンテナーは、構成ファイルで構成された要素に自動的に依存しますオブジェクトは、呼び出し元クラスのメンバー変数に挿入されます。
自動アセンブリを使用するには、要素のautowire属性を構成する必要があります。
次に例を示します。testbyName[Spring構成ファイルの合計Bean要素のid属性値は、呼び出し元クラスの依存オブジェクトのメンバー変数名と同じです]
注:依存オブジェクトにはsetXX()を指定する必要があります。
package com.wangxing.spring.byname;
//被调用者类
public class PersonDao {
public void select(){
System.out.println("查询所有用户信息");
}
}
package com.wangxing.spring.byname;
//调用者类
public class PersonService {
//定义依赖对象
private PersonDao personDao;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public void testPersonService(){
personDao.select();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 测试byname自动注入方式-->
<bean id="personDao" class="com.wangxing.spring.byname.PersonDao"></bean>
<bean id="personService" class="com.wangxing.spring.byname.PersonService" autowire="byName"></bean>
</beans>
次に例を示します。testbyType[Spring構成ファイルのBean要素のクラス属性値は、呼び出し元クラスの依存オブジェクトメンバー変数のタイプと同じ
です]注:依存オブジェクトにsetXX()を指定する必要があります。
package com.wangxing.spring.bytype;
//被调用者
public class StudentDao {
public void selectStudent(){
System.out.println("查询所有学生信息");
}
}
package com.wangxing.spring.bytype;
//调用者
public class StudentService {
//定义依赖对象
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public void testStudentService(){
studentDao.selectStudent();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--测试bytype自动注入方式-->
<bean id="stuDao" class="com.wangxing.spring.bytype.StudentDao"></bean>
<bean id="studentService" class="com.wangxing.spring.bytype.StudentService" autowire="byType"></bean>
</beans>
次に例を示します。テストコンストラクター[Spring構成ファイルのBean要素のクラス属性値は、呼び出し元クラス{byType}のコンストラクターのパラメータータイプと同じです]
package com.wangxing.spring.constructor;
//被调用者
public class UserDao {
public void selectUser(){
System.out.println("查询所有用户信息");
}
}
package com.wangxing.spring.constructor;
//调用者类
public class UserService {
private UserDao userDao;
public UserService(UserDao userDao){
this.userDao=userDao;
}
public void testUserService(){
userDao.selectUser();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--测试constructor自动注入方式-->
<bean id="uDao" class="com.wangxing.spring.constructor.UserDao"></bean>
<bean id="userService" class="com.wangxing.spring.constructor.UserService" autowire="constructor"></bean>
</beans>
3.アノテーションに基づくBeanインジェクション
Springでは、XML構成ファイルを使用することでBeanのアセンブリを実現できますが、アプリケーション内のBeanの数が多いと、XML構成ファイルが肥大化しすぎて、次のような問題が発生します。メンテナンスとアップグレード。
JavaはJDK5.0以降、アノテーション(アノテーション)機能を提供しており、Springはアノテーションテクノロジーの包括的なサポートも提供しています。一連のアノテーション(アノテーション)はSpring3で定義されています
。1)@Componentはオブジェクトのアノテーションを作成します。これを使用する場合は、対応するクラスにアノテーションをマークするだけで済みます。
@Componentの前
public class Student{
}
Spring配置文件
<bean id=”student” class=”com.wangxing.spring.bean.Student”></bean>
有@Component之后
@Component----使用默认的对象名称【类名,首字母小写】
public class Student{
}
@Component(“stu”)---使用指定的对象名称
public class Student{
}
例えば:
package com.wangxing.spring.annotation;
import org.springframework.stereotype.Component;
@Component("user")
public class UserBean {
public void testUserBean(){
System.out.println("UseerBean类的是实例方法");
}
}
<?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
http://www.springframework.org/schema/context/spring-context.xsd">
<!--使用context命名空间,通知spring扫描指定目录,进行注解的解析-->
<context:component-scan base-package="com.wangxing.spring.annotation"></context:component-scan>
</beans>
2)@Repositoryは、データアクセスインターフェイス実装クラスに注釈を付けます。その機能は@Componentと同じです。
3)@Serviceは、ビジネスアクセスインターフェイス実装クラスをマークし、その機能は@Componentと同じです。
4)@Controllerは、コントロールレイヤー実装クラスに注釈を付けます。その機能は@Componentと同じです。
5)@Autowiredは、Beanの自動注入を完了します。デフォルトはbytypeです。
package com.wangxing.spring.annotation;
import org.springframework.stereotype.Component;
@Component("user")
public class UserBean {
public void testUserBean(){
System.out.println("UseerBean类的是实例方法");
}
}
package com.wangxing.spring.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository("userDao")
public class UserDao {
//定义依赖对象UserBean
@Autowired
private UserBean userBean;
public void testUserDao(){
System.out.println("数据访问层接口实现类");
userBean.testUserBean();
}
}
6)@ResourceはBeanの自動注入を完了します。デフォルトは名前によるものです。
package com.wangxing.spring.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository("uDao")
public class UserDao {
//定义依赖对象UserBean
@Autowired
private UserBean user;
public void testUserDao(){
System.out.println("数据访问层接口实现类");
user.testUserBean();
}
}
package com.wangxing.spring.annotation.service;
import com.wangxing.spring.annotation.UserDao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("userService")
public class UserService {
//定义依赖对象UserDao
@Resource
private UserDao userDao;
public void testUserService(){
System.out.println("业务层接口实现类");
userDao.testUserDao();
}
}
@autowiredアノテーションと@resourceアノテーションの違いは何ですか?
(1)@Autowiredと@Resourceの両方を使用してBeanをアセンブルでき、両方ともフィールドまたはセッターメソッドに書き込むことができます。
(2)@Autowiredはデフォルトでタイプごとにアセンブルされます。 、および依存オブジェクトはデフォルトで必須である必要があります。null値を許可する場合は、必須属性をfalseに設定できます。名前アセンブリを使用する場合は、@ Qualifierアノテーションと組み合わせて使用できます。
(3)@Resource、デフォルトでは名前に従ってアセンブルされます。名前はname属性で指定できます。name属性が指定されていない場合、フィールドにアノテーションが書き込まれると、デフォルトでフィールド名が使用されます。名前検索用。アノテーションがsetterメソッドで記述されている場合、デフォルトでは属性名がアセンブリに使用されます。名前に一致するBeanが見つからない場合は、タイプに応じてアセンブリが実行されます。ただし、name属性が指定されている場合は、名前に従ってのみアセンブルされることに注意してください。
セッターメソッドを記述する必要がないように、フィールドで@Resourceアノテーションを使用することをお勧めします。このアノテーションは、J2EEに属し、Springとの結合を減らします。
7)@Qualifierアノテーションと@Autowiredアノテーションを使用すると、Beanタイプ別のデフォルトのアセンブリが、Beanのインスタンス名によるアセンブリに変更されます。Beanのインスタンス名は、@ Qualifierアノテーションのパラメーターで指定されます。
public interfase UserDao{
}
@Repository(“studentUsetDao”)
public class StudentUserDao implements UserDao{
}
@Repository(“personUsetDao”)
public class PersonUserDao implements UserDao{
}
//业务类
public class UserServiceImple{
@Autowired
@Qualifier(“personUsetDao”)
private UserDao userDao;
}