L'injection d'annotations crée des objets
L'injection d'annotation de ressort est un moyen plus courant
- Composant (fondamentalement identique à l'utilisation des trois suivants)
- Contrôleur (couche de présentation)
- Service (couche métier)
- Référentiel (couche de persistance)
Avant d'utiliser les annotations, vous devez modifier les informations de configuration du bean.xml d'origine
<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>
Commencez par introduire le premier composant d'annotation
/**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+".");
}
}