注解方式可以簡(jiǎn)化spring的IOC容器的配置!
使用注解步驟:
- 1)先引入context名稱空間
xmlns:context="http://www.springframework.org/schema/context" - 2)開(kāi)啟注解掃描
<context:component-scan base-package="cn.itcast.e_anno2"></context:component-scan> - 3)使用注解
通過(guò)注解的方式齿梁,把對(duì)象加入ioc容器催植。
創(chuàng)建對(duì)象以及處理對(duì)象依賴關(guān)系,相關(guān)的注解:
@Component
指定把一個(gè)對(duì)象加入IOC容器
@Repository
作用同@Component
勺择; 在持久層使用
@Service
作用同@Component
创南; 在業(yè)務(wù)邏輯層使用
@Controller
作用同@Component
; 在控制層使用
@Resource
屬性注入
總結(jié):
1) 使用注解省核,可以簡(jiǎn)化配置稿辙,且可以把對(duì)象加入IOC容器,及處理依賴關(guān)系(DI)
2) 注解可以和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:p="http://www.springframework.org/schema/p"
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 " >
<!--開(kāi)啟注解掃描-->
<context:component-scan base-package="com.huan"></context:component-scan>
</beans>
UserDao.java
package com.huan.BeanTest;
import org.springframework.stereotype.Component;
/**
* Created by 馬歡歡 on 2017/6/24.
*/
@Component("userDao") //相當(dāng)于 bean.xml[<bean id =userDao class="com.huan.BeanTest.UserDao">]
public class UserDao {
public void save(){
System.out.println("db:操作數(shù)據(jù)庫(kù)");
}
}
UserService.java
package com.huan.BeanTest;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* Created by 馬歡歡 on 2017/6/24.
*/
@Component("userService")//userService對(duì)象气忠,注入到ioc容器
public class UserService {
//從IOC容器中找到userDao對(duì)象邻储,注入到當(dāng)前字段
/**
* <bean id =userDao class="com.huan.BeanTest.UserDao">
* <property name = "userDao" ref ="userDao" ></property> 相當(dāng)于 @Resource(name = "userDao")
*
* </bean>
*/
@Resource(name = "userDao")
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save(){
userDao.save();
}
}
UserAction.java
package com.huan.BeanTest;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* Created by 馬歡歡 on 2017/6/24.
*/
@Component("userAction")
public class UserAction {
@Resource(name = "userService")
private UserService userService;
public void execute(){
userService.save();
}
}
創(chuàng)建user對(duì)象
@Test
public void test2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
UserAction userAction = (UserAction) applicationContext.getBean("userAction");
userAction.execute();
方法二
注解:
<?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:p="http://www.springframework.org/schema/p"
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 " >
<!--開(kāi)啟注解掃描-->
<context:component-scan base-package="com.huan"></context:component-scan>
</beans>
UserDao.java
package com.huan.BeanTest;
import org.springframework.stereotype.Component;
/**
* Created by 馬歡歡 on 2017/6/24.
*/
@Component
public class UserDao {
public void save(){
System.out.println("db:操作數(shù)據(jù)庫(kù)zui");
}
}
UserService.java
package com.huan.BeanTest;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* Created by 馬歡歡 on 2017/6/24.
*/
@Component
public class UserService {
@Resource//根據(jù)類型查找·[在容器中,該類型只有一個(gè)變量]
private UserDao userDao;
public void save(){
userDao.save();
}
}
UserAction.java
package com.huan.BeanTest;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* Created by 馬歡歡 on 2017/6/24.
*/
@Component
public class UserAction {
@Resource
private UserService userService;
public void execute(){
userService.save();
}
}
創(chuàng)建user對(duì)象
@Test
public void test2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
UserAction userAction = (UserAction) applicationContext.getBean("userAction");
userAction.execute();
方法三
注解:
<?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:p="http://www.springframework.org/schema/p"
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 " >
<!--開(kāi)啟注解掃描-->
<context:component-scan base-package="com.huan"></context:component-scan>
</beans>
UserDao.java
package com.huan.BeanTest;
import org.springframework.stereotype.Repository;
/**
* Created by 馬歡歡 on 2017/6/24.
*/
@Repository
public class UserDao {
public void save(){
System.out.println("db:操作數(shù)據(jù)庫(kù)zui");
}
}
UserService.java
package com.huan.BeanTest;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* Created by 馬歡歡 on 2017/6/24.
*/
@Service
public class UserService {
@Resource
private UserDao userDa;
public void save(){
userDa.save();
}
}
UserAction.java
package com.huan.BeanTest;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
/**
* Created by 馬歡歡 on 2017/6/24.
*/
@Controller
public class UserAction {
@Resource
private UserService userService;
public void execute(){
userService.save();
}
}
創(chuàng)建user對(duì)象
@Test
public void test2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
UserAction userAction = (UserAction) applicationContext.getBean("userAction");
userAction.execute();
上一篇:Spring--IOC容器——對(duì)象依賴關(guān)系(構(gòu)造函數(shù)旧噪、set方法吨娜、p名稱空間、自動(dòng)裝配)