Spring基本用法
1、使用maven导入spring-context依赖:示例的maven的pom.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<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.xsy</groupId>
<artifactId>SpringLearn</artifactId>
<version>1.0-SNAPSHOT</version><dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
</dependencies></project>
2、IOC创建对象
首先创建一个Bean类,示例如下所示
package com.xsy; public class Apple { private String type; private Integer value; public Apple() { } public Apple(String type, Integer value) { this.type = type; this.value = value; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Integer getValue() { return value; } public void setValue(Integer value) { this.value = value; } @Override public String toString() { return "Apple{" + "type='" + type + '\'' + ", value=" + value + '}'; } }
自动装载这个对象的方式有如下几种
2.1通过xml中配置
配置的xml示例如下所示,放在Config/Config.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"> <bean class="com.xsy.Apple" id="myapple"> <property name="type" value="蛇果"></property> <property name="value" value="12"></property> </bean> </beans>
java代码通过SpringIOC功能获取对象实例
ApplicationContext ac=new ClassPathXmlApplicationContext("Config/Config.xml"); Apple bean= (Apple) ac.getBean("myapple");
2.2通过java配置类进行Spring对象配置
先写一个配置类
package com.xsy.config; import com.xsy.Apple; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration//告诉Spring这是个配置类 public class SpringConfig { //@Bean//默认以方法名作为这个Bean在SpringIOC中的变量名称(id) @Bean(value="myappleBean")//定义这个Bean在SpringIOC中的变量名称(id) public Apple myapple(){ return new Apple("蛇果",12); } }
java代码通过SpringIOC功能获取对象实例
ApplicationContext ac= new AnnotationConfigApplicationContext(SpringConfig.class); Apple bean= ac.getBean(Apple.class);
2.3通过自动扫描包中的注解进行初始化对象
@ComponentScan("com.xsy.bean")
其中注解包括@Controller,@Service,@Repository,@Component四种
例如创建Banana类,注解标注为Repository
package com.xsy.bean; import org.springframework.stereotype.Repository; @Repository public class Banana { }
2.3.1自动扫描创建对象的精确控制范围
在设置包的时候,如果不想创建某些对象,则需要对创建对象的范围进行精确地控制
例如创建了4个不同的注解的类
package com.xsy.bean; import org.springframework.stereotype.Controller; @Controller public class BeanTestController { }
package com.xsy.bean; import org.springframework.stereotype.Service; @Service public class BeanTestService { }
package com.xsy.bean; import org.springframework.stereotype.Component; @Component public class BeanTestComponent { }
package com.xsy.bean; import org.springframework.stereotype.Repository; @Repository public class BeanTestRepository { }
如果不想创建某个包中的被@Controller和@Service修饰的对象,那么可以在配置文件中进行如下配置
@ComponentScan(value = "com.xsy.bean",excludeFilters ={ @ComponentScan.Filter(type= FilterType.ANNOTATION,value = {Controller.class, Service.class}) })
使用@ComponentScan注解的excludeFilters字段,excludeFilters的value是@ComponentScan.Filter注解,@ComponentScan.Filter注解需要其对应的过滤的type类型和不需要的class。
如果要配置多个ComponentScan,则在配置文件中使用@ComponentScans注解即可
@ComponentScans({ @ComponentScan(value = "com.xsy.bean",excludeFilters ={ @ComponentScan.Filter(type= FilterType.ANNOTATION,value = {Controller.class, Service.class}) }) })
因为@Controller、@Service和@Repository注解均被@Component注解,因此excludeFilters中添加了Component后,即将这四个全部过滤,都不创建对象。即excludeFilters的过滤是只要满足即过滤。
如果只需要包含@Repository注解的对象,则需要使用includeFilters,并且将useDefaultFilters这个字段的值设置为false,即不能使用默认的过滤方式,否则会导致包中所有的对象都会被初始化,而没有达到只初始化某些类型的对象的要求
@ComponentScans({ @ComponentScan(value = "com.xsy.bean",includeFilters ={ @ComponentScan.Filter(type= FilterType.ANNOTATION,value = {Repository.class}) },useDefaultFilters = false) })