Annotation injection creates objects
Spring annotation injection is a more common way
- Component (basically the same as the use of the following three)
- Controller (presentation layer)
- Service (business layer)
- Repository (persistence layer)
Before using annotations, you need to modify the configuration information of the original 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>
First introduce the first annotation Component
/**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+".");
}
}