Développement d'annotations dans Spring (pas de configuration XML)

1. Préparation: importation d'un package AOP

2.beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
       
        <!--开启注解的支持-->
    <context:annotation-config/>
    <!--指定要扫描的包,这个包下的注解会生效-->
    <context:component-scan base-package="com.tt"/>
</beans>

3. Injection d'attribut

3.1 Première méthode

//@Component 组件等价于<bean id="user" class="com.tt.pojo.User"/>
@Component
//设置作用域
@Scope("singleton")
public class User {
    public String name="甜甜";
    public void setName(String name){
        this.name = name;
    }
}

3.2 Deuxième méthode

//@Component 组件等价于<bean id="user" class="com.tt.pojo.User"/>
@Component
public class User {
    public String name;
    //@Value相当于<property name="name" value="甜甜"/>
    @Value("甜甜")
    public void setName(String name){
        this.name = name;
    }
}

Développer

@Component a plusieurs annotations dérivées. Dans le développement web, nous allons superposer selon l'architecture à trois couches de mvc.
Ces quatre fonctions d'annotation sont les mêmes, juste pour la représentation en couches

  • dao 层 【@Repository】
@Repository
public interface UserDao {
}
  • couche de service 【@Service】
@Service
public interface UserService {
}
  • contrôleur 层 【@Controller】
@Controller
public class UserController {
}
A publié 51 articles originaux · Aime 73 · Visites 3700

Je suppose que tu aimes

Origine blog.csdn.net/qq_41256881/article/details/105427630
conseillé
Classement