spring5.x版本 java配置和annotation配置說明
java配置
@Configuration //這個註解的意思是相當於把此類當作一個xml檔案
public class AppConfig {
@Bean(name="userService") //這個註解相當於xml檔案裡的<bean>
public UserService userService() {
UserService service = new UserService();
service.setUserDAO(userDAOIml());
return service;
}
@Bean
public UserDAOIml userDAOIml() {
return new UserDAOIml();
}
}
public class UserServiceTest {
@Test
public void testAdd() {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
// UserService service = (UserService)context.getBean(UserService.class);
UserService service = (UserService)context.getBean("userService");
User user = new User();
user.setName("xxx");
user.setPassword("123456");
service.add(user);
}
}
這樣就不需要XML檔案了。直接用類名或者Bean名都可以獲取。
Annotation配置
首先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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 開啟annotation配置 -->
<context:annotation-config/>
<!-- 掃描以com開頭的包裡邊的bean -->
<context:component-scan base-package="com"/>
</beans>
接著用@Repository配置這兩個實現同一個介面的DAO。
@Repository
public class UserDAOIml implements UserDAO {
@Override
public void save(User user) {
// TODO Auto-generated method stub
System.out.println("UserDAOIml: -----save a user");
}
}
@Repository
public class UserDAOIml2 implements UserDAO {
@Override
public void save(User user) {
// TODO Auto-generated method stub
System.out.println("UserDAOIml2: -----save a user");
}
}
接下來配置呼叫的類
@Service
public class UserService {
@Autowired
@Qualifier("userDAOIml2") //如果沒有這句就會報錯,Spring不知道該注入哪個實現類
private UserDAO userDAO;
public UserService() {
// TODO Auto-generated constructor stub
System.out.println("初始化");
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public void add(User user) {
userDAO.save(user);
}
}
試過不加@Service也能執行成功,暫時不知道啥情況
呼叫方法
@Test
public void testAdd() {
ApplicationContext context = new ClassPathXmlApplicationContext("benasAnnotation.xml");
UserService service = (UserService)context.getBean(UserService.class);
User user = new User();
user.setName("xxx");
user.setPassword("123456");
service.add(user);
}
}
Spring提供四種配置bean的註解。目前測試暫時效果都一樣的
@Component @Controller @Service @Repository 回頭用的時候再補充吧
相關文章
- Kafka配置說明Kafka
- FTP配置說明FTP
- rust配置說明Rust
- 【MySQL】SemisynchronousReplication配置和引數說明MySql
- java開發環境配置說明書Java開發環境
- Spring配置說明Spring
- 【MySQL】Semisynchronous Replication 配置和引數說明MySql
- java WebSocket 即時通訊配置使用說明JavaWeb
- elasticsearch.yml 配置說明Elasticsearch
- Elasticsearch 引數配置說明Elasticsearch
- goldengate ddl 配置說明Go
- kafka 引數配置說明Kafka
- webwork.properties配置說明Web
- 交換機配置命令說明
- kettle MongoDB Output 配置說明MongoDB
- pureftpd安裝配置簡明說明 (轉)FTP
- Redis應用配置項說明Redis
- Nginx的配置檔案說明Nginx
- Docker 的配置檔案說明Docker
- Nginx負載均衡配置說明Nginx負載
- Apache 配置檔案說明(轉)Apache
- 計劃任務工具 cron 的配置和說明
- Java虛擬機器(JVM)引數配置說明薦Java虛擬機JVM
- (轉)計劃任務工具 cron 的配置和說明
- Nginx的gzip配置引數說明Nginx
- nginx 詳解 – 詳細配置說明Nginx
- nginx 詳解 - 詳細配置說明Nginx
- VNC安裝配置詳細說明VNC
- 【轉載】UEditor前端配置項說明前端
- Nginx配置檔案詳細說明Nginx
- Spring配置項解釋說明Spring
- saltstack/saltmaster配置檔案說明(二)AST
- Redis配置檔案引數說明Redis
- 關於NTP SERVER的配置說明Server
- keycloak~token配置相關說明
- Revit Server的注意要配置說明Server
- Java Out Of Memory解決之JAVA_OPTS引數說明與配置Java
- openssh版本更新與說明 openssl版本更新與說明