アノテーションインジェクションはオブジェクトを作成します
Springアノテーションインジェクションはより一般的な方法です
- コンポーネント(基本的に次の3つの使用と同じ)
- コントローラー(プレゼンテーション層)
- サービス(ビジネス層)
- リポジトリ(永続層)
アノテーションを使用する前に、元のbean.xmlの構成情報を変更する必要があります
<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:component-scan base-package="imis"></context:component-scan>
</beans>
最初に最初の注釈コンポーネントを導入します
/**Component
作用:用于把当前类对象存入spring容器中
属性:
value:用于指定bean的id,当不写时,默认为当前的类名,首字母改为小写
*/
@Component
public class UserServiceImpl implements IUserService {
private String name;
private Integer age;
private Date birthday;
public UserServiceImpl() {
}
public UserServiceImpl(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
public void queryAll() {
System.out.println(name+","+age+","+birthday+".");
}
}