Injection d'annotation de ressort (4)

L'injection d'annotations crée des objets

L'injection d'annotation de ressort est un moyen plus courant

  1. Composant (fondamentalement identique à l'utilisation des trois suivants)
  2. Contrôleur (couche de présentation)
  3. Service (couche métier)
  4. 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+".");
    }
}

Je suppose que tu aimes

Origine blog.csdn.net/weixin_45925906/article/details/112748256
conseillé
Classement