接上一篇 IOC配置方法:http://blog.csdn.net/gw85047034/article/details/52768780
本文通过几个简单的类,验证一下@Component,@Service,@Repository,@Controller,@Autowired这几个注解的使用
1、User.java
使用@Component
package com.annotation;
import org.springframework.stereotype.Component;
@Component
public class User {
public User() {
super();
System.out.println("User's constructor...");
}
}
2、UserRepository
使用接口类和实现类,并且使用value属性,对bean进行命名;
引用User类,使用Autowired自动装载
2.1 UserRepository接口类
package com.annotation.respository;
public interface UserRepository {
void save();
}
2.2 UserRepositoryImpl实现类
package com.annotation.respository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.annotation.User;
// 此处用value属性对bean重命名,否则默认名称就是userRepositoryImpl
@Repository("UserRepository")
public class UserRepositoryImpl implements UserRepository {
User user;
public User getUser() {
return user;
}
// 此处在setter的参数位置,添加@Autowired自动装载bean
public void setUser(@Autowired User user) {
this.user = user;
}
@Override
public void save() {
System.out.println("UserRepository save...");
}
}
3、UserService
使用@Service
引用UserRepository类,使用@Autowired自动装载
package com.annotation.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.annotation.respository.UserRepository;
@Service
public class UserService {
// 此处在属性上增加@Autowired,实现自动装载
@Autowired
private UserRepository userRepository;
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
public UserRepository getUserRepository() {
return userRepository;
}
public void add() {
System.out.println("UserService add...");
userRepository.save();
}
}
4、UserController
使用@Controller
引用UserService类,使用@Autowired自动装载
package com.annotation.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.annotation.service.UserService;
@Controller
public class UserController {
private UserService userService;
public UserService getUserService() {
return userService;
}
// 此处在setter上增加 @Autowired
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
public void execute() {
System.out.println("UserController execute...");
userService.add();
}
}
PS:@Autowired可以在三个地方添加,属性上面,Setter上面以及setter的参数前面
5、配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan
base-package="com.annotation">
</context:component-scan>
</beans>
6、main函数
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
UserController userController = (UserController) context.getBean("userController");
userController.execute();
}
}
执行结果
User's constructor...
UserController execute...
UserService add...
UserRepository save...