Annotation development in Spring (no XML configuration)

1. Preparation: Import aop package

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. Attribute injection

3.1 Method One

//@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 Method two

//@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;
    }
}

expand

@Component has several derivative annotations. In web development, we will layer according to the three-layer architecture of mvc.
These four annotation functions are the same, just for layered representation.

  • dao 层 【@Repository】
@Repository
public interface UserDao {
}
  • service layer 【@Service】
@Service
public interface UserService {
}
  • controller层【@Controller】
@Controller
public class UserController {
}
Published 51 original articles · Likes 73 · Visits 3700

Guess you like

Origin blog.csdn.net/qq_41256881/article/details/105427630