package com.zhangguo.Spring051.ioc01;
/**
* 图书数据访问接口
*/
public interface IBookDAO {
/**
* 添加图书
*/
public String addBook(String bookname);
}
package com.zhangguo.Spring051.ioc01;
/**
* 图书数据访问实现类
*/
public class BookDAO implements IBookDAO {
public String addBook(String bookname) {
return "添加图书"+bookname+"成功!";
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhangguo</groupId>
<artifactId>Spring051</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring051</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.0.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency>
</dependencies>
</project>
package com.zhangguo.Spring051.ioc01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 图书业务类
*/
public class BookService {
IBookDAO bookDAO;
public BookService() {
//容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("IOCBeans01.xml");
//从容器中获得id为bookdao的bean
bookDAO=(IBookDAO)ctx.getBean("bookdao");
}
public void storeBook(String bookname){
System.out.println("图书上货");
String result=bookDAO.addBook(bookname);
System.out.println(result);
}
}
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="bookdao" class="com.zhangguo.Spring051.ioc01.BookDAO"></bean> </beans>
package com.zhangguo.Spring051.ioc01;
public class Test {
@org.junit.Test
public void testStoreBook()
{
BookService bookservice=new BookService();
bookservice.storeBook("《Spring MVC权威指南 第一版》");
}
}
package com.zhangguo.Spring051.ioc02;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
/**
* 图书数据访问实现类
*/
@Component("bookdaoObj")
public class BookDAO implements IBookDAO {
public String addBook(String bookname) {
return "添加图书"+bookname+"成功!";
}
}
package com.zhangguo.Spring051.ioc02;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
* 图书业务类
*/
@Component
public class BookService {
IBookDAO bookDAO;
public void storeBook(String bookname){
//容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("IOCBeans02.xml");
//从容器中获得id为bookdao的bean
bookDAO=(IBookDAO)ctx.getBean("bookdaoObj");
System.out.println("图书上货");
String result=bookDAO.addBook(bookname);
System.out.println(result);
}
}
<?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-4.3.xsd"> <context:component-scan base-package="com.zhangguo.Spring051.ioc02"></context:component-scan> </beans>
<!-- 自动扫描com.zhangguo.anno.bo中的类进行扫描 --> <context:component-scan base-package="com.zhangguo.anno" resource-pattern="bo/*.class" /> <context:component-scan base-package="com.zhangguo.anno" > <context:include-filter type="aspectj“ expression="com.zhangguo.anno.dao.*.*"/> <context:exclude-filter type=“aspectj” expression=“com.zhangguo.anno.entity.*.*”/> </context:component-scan>
| [b]Filter Type[/b] | [b]Examples Expression[/b] | [b]Description[/b] |
| annotation | org.example.SomeAnnotation | 注解了SomeAnnotation的类 |
| assignable | org.example.SomeClass | 所有扩展或者实现SomeClass的类 |
| aspectj | org.example..*Service+ | AspectJ语法表示org.example包下所有包含Service的类及其子类 |
| regex | org\.example\.Default.* | Regelar Expression,正则表达式 |
| custom | org.example.MyTypeFilter | 通过代码过滤,实现org.springframework.core.type.TypeFilter接口 |
<!-- 1、如果仅希望扫描特定的类而非基包下的所有类,可使用resource-pattern属性过滤特定的类 --> <context:component-scan base-package="com.zhangguo.Spring051" resource-pattern="ioc04/A*.class"> </context:component-scan>
<!--2、扫描注解了org.springframework.stereotype.Repository的类 exclude-filter表示排除,include-filter表示包含,可以有多个--> <context:component-scan base-package="com.zhangguo.Spring051.ioc04"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> </context:component-scan>
<!--3、aspectj类型,扫描dao下所有的类,排除entity下所有的类--> <context:component-scan base-package="com.zhangguo.anno" > <context:include-filter type="aspectj" expression="com.zhangguo.anno.dao.*.*"/> <context:exclude-filter type="aspectj" expression="com.zhangguo.anno.entity.*.*"/> </context:component-scan>
package com.zhangguo.Spring051.ioc02;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void testStoreBook()
{
//容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("IOCBeans02.xml");
BookService bookservice=ctx.getBean(BookService.class);
bookservice.storeBook("《Spring MVC权威指南 第二版》");
}
}
package com.zhangguo.Spring051.ioc03;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
/**
* 图书数据访问实现类
*/
@Repository
public class BookDAO implements IBookDAO {
public String addBook(String bookname) {
return "添加图书"+bookname+"成功!";
}
}
package com.zhangguo.Spring051.ioc03;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
/**
* 图书业务类
*/
@Service
public class BookService {
@Autowired
IBookDAO bookDAO;
public void storeBook(String bookname){
System.out.println("图书上货");
String result=bookDAO.addBook(bookname);
System.out.println(result);
}
}
package com.zhangguo.Spring051.ioc05;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
/**
* 图书业务类
*/
@Service
public class BookService {
public IBookDAO getDaoofbook() {
return daoofbook;
}
/*
@Autowired
@Qualifier("bookdao02")
public void setDaoofbook(IBookDAO daoofbook) {
this.daoofbook = daoofbook;
}*/
@Resource(name="bookdao02")
public void setDaoofbook(IBookDAO daoofbook) {
this.daoofbook = daoofbook;
}
/*
@Autowired
@Qualifier("bookdao02")
*/
IBookDAO daoofbook;
/*
public BookService(@Qualifier("bookdao02") IBookDAO daoofbook) {
this.daoofbook=daoofbook;
}*/
public void storeBook(String bookname){
System.out.println("图书上货");
String result=daoofbook.addBook(bookname);
System.out.println(result);
}
}
package com.zhangguo.Spring051.ioc03;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void testStoreBook()
{
//容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("IOCBeans03.xml");
BookService bookservice=ctx.getBean(BookService.class);
bookservice.storeBook("《Spring MVC权威指南 第三版》");
}
}
package com.zhangguo.Spring051.ioc06;
/**
* 图书数据访问接口
*/
public interface IBookDAO {
/**
* 添加图书
*/
public String addBook(String bookname);
}
package com.zhangguo.Spring051.ioc06;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
/**
* 图书数据访问实现类
*/
@Repository
public class BookDAO implements IBookDAO {
public String addBook(String bookname) {
return "添加图书"+bookname+"成功!";
}
}
package com.zhangguo.Spring051.ioc06;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
/**
* 图书业务类
*/
@Service
public class BookService {
@Resource
IBookDAO bookDAO;
public void storeBook(String bookname){
System.out.println("图书上货");
String result=bookDAO.addBook(bookname);
System.out.println(result);
}
}
package com.zhangguo.Spring051.ioc06;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* 容器的配置类
*/
@Configuration
@ComponentScan(basePackages="com.zhangguo.Spring051.ioc06")
public class ApplicationCfg {
@Bean
public User getUser(){
return new User("成功");
}
}
package com.zhangguo.Spring051.ioc06;
import org.springframework.stereotype.Component;
@Component("user1")
public class User {
public User() {
System.out.println("创建User对象");
}
public User(String msg) {
System.out.println("创建User对象"+msg);
}
public void show(){
System.out.println("一个学生对象!");
}
}
package com.zhangguo.Spring051.ioc06;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
@org.junit.Test
public void testStoreBook()
{
//容器,注解配置应用程序容器,Spring通过反射ApplicationCfg.class初始化容器
ApplicationContext ctx=new AnnotationConfigApplicationContext(ApplicationCfg.class);
BookService bookservice=ctx.getBean(BookService.class);
bookservice.storeBook("《Spring MVC权威指南 第四版》");
User user1=ctx.getBean("user1",User.class);
user1.show();
User getUser=ctx.getBean("getUser",User.class);
getUser.show();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有