まず、春のIOCを使用するステップの注釈に基づきます
1、Mavenのプロジェクトおよび輸入座標を作成します
エンティティを作成します。2.
package com.wedu.spring03.domain;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.Date;
/**
* 用户实体
*/
@Component
public class User implements Serializable {
private Integer id;
private String username;
private String address;
private String sex;
private Date birthday;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
package com.wedu.spring03.domain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.io.Serializable;
/**
* 账户实体
*/
@Component("account") //创建对象
@Scope("singleton") //作用域
public class Account implements Serializable {
@Value("1")
private Integer id;
@Value("46")
private Integer uid;
@Value("10000") //向基本类型注入数据
private Double money;
//@Autowired //自动按照类型注入。
//@Qualifier("user") //在按照类中注入的基础之上再按照名称注入
@Resource(name = "user") //直接按照bean的id注入
private User user;
public void printUser() {
System.out.println(user);
}
@PostConstruct //用于指定初始化方法
public void init(){
System.out.println("初始化方法执行了");
}
@PreDestroy //用于指定销毁方法
public void destroy(){
System.out.println("销毁方法执行了");
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", uid=" + uid +
", money=" + money +
'}';
}
}
3、プロファイルbean.xmlを作成します
<?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">
<!--告知spring在创建容器时要扫描的包-->
<context:component-scan base-package="com.wedu.spring03.domain"/>
</beans>
4、テストクラスを作成します
package com.wedu.spring03.domain;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 基于注解的spring ioc的使用
*/
public class AccountTest {
@Test
public void annotationTest() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
Account account = ac.getBean("account",Account.class);
System.out.println(account);
account.printUser();
Account account2 = ac.getBean("account",Account.class);
System.out.println(account2);
ac.close();
}
}
二、共通の注釈IOC春
図1に示すように、オブジェクトの注釈を作成します
@成分
- 春のリソースが管理されます。XML Bean内の設定に相当します。
- ビーンIDを指定する値。あなたはvalue属性を指定しない場合は、現在のクラスのデフォルトの豆のIDは、クラス名です。最初の文字は小文字。
@コントローラ
- @Componentとプレゼンテーション層のための一般的なノート、
@サービス
- @Componentとのビジネス層のための一般的なノート、
@Repository
- @Componentと永続化層のための一般的なノート、
2、スコープノート
@Scope:範囲指定した範囲。
3、ライフサイクルのコメント
@PostConstruct
- 指定春のコンテナを初期化
@PreDestroy
- Springコンテナ特定の破棄方法
注4、インジェクション法
@Autowired
- 自動注射器のタイプに応じて、それだけ他のBeanタイプ噴射することができます。
- この方法は、移植前に設定されている場合は、前者の方法に注釈を追加したり、設定された属性に直接追加することができます;サポートコンストラクタ・インジェクション方式と注入セット
- 注射の種類に応じて@Autowired、一緒に使用されるよりアドバイスと@Qualifier注釈を検索明示的ビーンIDが注入される特定することができます。
@Qualifier
- value属性を使用すると、豆のIDを指定します。
- 自動注射に基づいて上記の種類に応じて、豆リフィルIDに従って。フィールド注入は、@Autowireと一緒に使用しなければならない場合には、独立して使用することはできません。しかし、パラメータの注入方法は、独立して使用することができるとき。
@資源
- 他のBeanタイプ噴射のみ。
- 豆のIDを注射するname属性の指定を使用して、集合注入法をサポートしています。
@値
- インジェクションと関連する注釈String型の値の基本的なタイプ。
- この属性は、元のプロパティに追加することができ、以前の設定方法に加えてもよいです。
- 注入のコレクション型のみがXMLを介して達成することができることに注意してください。