Spring的bean管理(註解注入屬性和註解檔案混合使用)
阿新 • • 發佈:2019-02-07
Spring的bean管理(註解注入屬性和註解檔案混合使用)
註解方式注入屬性
1 建立service類,建立dao類,在service得到dao物件
注入屬性第一個註解 @Autowired
(1) 建立dao和service物件
(2) 在service類裡面定義到型別屬性
注入屬性第一個註解 @Autowired
注入屬性第二個註解@Resource
具體程式碼例項 第一步加註解掃描:bean1.xml
UsersDao.java<?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"> <!-- 開啟註解掃描 (1) 到包裡面掃描類 方法 屬性上面是否有註解 --> <context:component-scan base-package="com.cn.anno"></context:component-scan> </beans>
package com.cn.anno;
import org.springframework.stereotype.Service;
@Service(value="userDao")
public class UsersDao {
public void add(){
System.out.println("dao..........");
}
}
UserServlce.javapackage com.cn.anno; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component(value="userService") public class UserServlce { //得到dao物件 //定義dao型別屬性 //在dao屬性上面使用註解 完成物件注入 // @Autowired // private UsersDao userDao; //使用註解方式時候,不需要set方法 //name屬性值寫註解建立dao物件 @Resource(name="userDao") private UsersDao userDao; public void add(){ System.out.println("servlice........"); userDao.add(); } }
package com.cn.anno; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAnno { @Test public void testService(){ ApplicationContext context= new ClassPathXmlApplicationContext("bean1.xml"); UserServlce user=(UserServlce) context.getBean("userService"); user.add(); } }
測試結果
下面講(註解和配置檔案混合使用) 配置檔案和註解混合使用
1.建立物件操作使用配置檔案方式實現
2 注入屬性的操作使用註解方式實現
具體程式碼實現 配置檔案 bean2.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">
<!-- 開啟註解掃描 (1) 到包裡面掃描類 方法 屬性上面是否有註解 -->
<context:component-scan base-package="com.cn.anno"></context:component-scan>
<!-- 配置物件 -->
<bean id="bookService" class="com.cn.xmlanno.BookService"></bean>
<bean id="bookDao" class="com.cn.xmlanno.BookDao"></bean>
<bean id="orderDao" class="com.cn.xmlanno.OrderDao"></bean>
</beans>
BookDao.java
package com.cn.xmlanno;
public class BookDao {
public void book(){
System.out.println("booksdao......");
}
}
OrderDao.java
package com.cn.xmlanno;
public class OrderDao {
public void buy(){
System.out.println("ordersdao......");
}
}
BookService.javapackage com.cn.xmlanno;
import javax.annotation.Resource;
public class BookService {
//得到bookdao和ordersdao物件
@Resource(name="bookDao")
private BookDao bookDao;
@Resource(name="orderDao")
private OrderDao orderDao;
public void add(){
System.out.println("service......");
bookDao.book();
orderDao.buy();
}
}
測試類TestAnno.javapackage com.cn.xmlanno;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnno {
@Test
public void testService(){
ApplicationContext context=
new ClassPathXmlApplicationContext("bean2.xml");
BookService book=(BookService) context.getBean("bookService");
book.add();
}
}
測試結果