1. 基于Xml 元数据模式(XML-based metadata)
1.1开发环境搭建(基于maven) a.在pom文件中引入IOC核心依赖
<!-- IOC 核心jar,引入该jar可以使用springIOC 90%的功能 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.23</version>
</dependency>
b.在类路径下创建spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
c.添加需要托管的JavaBean:
package cn.shanxincd.ih.service;
public class UserService {
public void sayHello(String name){
System.out.println("向"+name+"问好");
}
}
d.注册javaBean到spring容器中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<beans>
<bean id="userService" class="cn.shanxincd.ih.service.UserService">
</bean>
</beans>
</beans>
e.创建测试类测试
package cn.shanxincd.ih.test;
import cn.shanxincd.ih.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class XmlContainerTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext ct = new ClassPathXmlApplicationContext("spring.xml");
UserService userService = (UserService) ct.getBean("userService");
userService.sayHello("张三");
}
}
上面一个简单的入门已经实现了spring IOC基本的功能(将对象的管理交给spring容器,在使用时从spring容器中获取Bean实例)
2. 基于注解的配置( Annotation-based configuration)
2.1在开发模式1的基础上,在spring.xml中添加context的命名空间和约束,并添加开启注解扫描和包扫描(context:annotation-config</context:annotation-config>可以省略,spring5.0中已经和context:component-scan合并,添加包扫描即可)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="cn.shanxincd.ih"/>
<bean id="userService" class="cn.shanxincd.ih.service.UserService">
</bean>
</beans>
2.2添加JavaBean,通过@Component将该bean注册到spring容器中
package cn.shanxincd.ih.service;
import org.springframework.stereotype.Component;
@Component
public class UserService2 {
public void sayHello(String name){
System.out.println("UserService2 sayHello() 向"+name+"问好");
}
}
2.3测试调用
package cn.shanxincd.ih.test;
import cn.shanxincd.ih.service.UserService;
import cn.shanxincd.ih.service.UserService2;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class XmlContainerTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext ct = new ClassPathXmlApplicationContext("spring.xml");
UserService userService = (UserService) ct.getBean("userService");
userService.sayHello("张三");
System.out.println("通过注解管理的Bean测试======================");
UserService2 userService2 = (UserService2) ct.getBean("userService2");
userService2.sayHello("李四");
}
}
3. 基于Java config(Java-based configuration)
基于Java config的IOC开发,可以完全摆脱xml配置,改为完全由Java代码替代,这也是最简洁的一种IOC开发模式,强烈推荐 3.1将spring.xml配置文件删除或者修改后缀名称,不删除也可,应为这种模式下spring的启动容器和xml模式下spring的启动容器不是同一个,不会影响 3.2新建spring配置类:AppConfig.java,该类添加@Configuration和包扫描注解@ComponentScan
package cn.shanxincd.ih.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
@ComponentScan(value = "cn.shanxincd.ih")
public class AppConfig {
}
3.3将要托管给spring容器的JavaBean添加注解(@Repository、@Service、@Controller、@Component)
package cn.shanxincd.ih.service;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void sayHello(String name){
System.out.println("向"+name+"问好");
}
}
3.4添加测试类测试调用
package cn.shanxincd.ih.test;
import cn.shanxincd.ih.config.AppConfig;
import cn.shanxincd.ih.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ContainerTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = applicationContext.getBean(UserService.class);
userService.sayHello("张三");
}
}
使用AnnotationConfigApplicationContext类,将配置类传入AnnotationConfigApplicationContext构造器中执行spring注解容器启动操作
4.三种模式混合使用
4.1保留xml模式下spring.xml配置文件,在模式3的AppConfig配置类中引入spring.xml配置文件:@ImportResource(“classpath:spring.xml”) 4.2如果是在配置类中进行包扫描则在配置类中使用@ComponentScan(value = “cn.shanxincd.ih”)即可,如果是在配置文件中进行包扫描则在spring.xml中添加<context:component-scan base-package=“cn.shanxincd.ih”/> 标签即可,这两种方式任选其一即可;base-package中的字符串是要扫描的包名,使用中尽量缩小包扫描的范围,这样能提高程序执行的效率
package cn.shanxincd.ih.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
@Configuration
@ComponentScan(value = "cn.shanxincd.ih")
@ImportResource("classpath:spring.xml")
@MapperScan("cn.shanxincd.ih.mapper")
public class AppConfig {
}
|