基于JDK类型的注入:
因为他不知道properties中key值对应的value是数组还是list
为集合类型进行注入
init.properties:
id =10
name=suns
applicationContext.xml:中写入
<context:property-placeholder location="classpath:init.properties"/>
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--
id:名字唯一
class属性:类的全限定名
通过写bean标签告诉工厂生产的对象
-->
<!-- 让spring框架在设置的包及其子包中扫描对应的注解,使其生效-->
<context:component-scan base-package="com.baizhiedu"/>
<!-- 通过配置文件可以覆注解的内容-->
<bean id="u" class="com.baizhiedu.bean.User">
<property name="id" value="10"/>
</bean>
<!-- 读取配置文件-->
<!--<context:property-placeholder location="classpath:init.properties"/>-->
</beans>
Category:
package com.baizhiedu.injection;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component//注解指定创建的对象
@PropertySource("classpath:/init.properties")//注解替换在配置文件中的:<context:property-placeholder location="classpath:init.properties"/>标签
public class Category {
@Value("${id}")//加入Value注解为id属性新型注入赋值
private Integer id;
@Value("${name}")
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
JDKAnnotation
package com.baizhiedu;
import com.baizhiedu.injection.Category;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JDKAnnotation {
//测试JDK注入 Value注入
@Test
public void test1(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Category category=(Category)ctx.getBean("category");
System.out.println("category.getId()="+category.getId());
System.out.println("category.getName="+category.getName());
}
//测试spring注入产生的对象的id
@Test
public void test2(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
String [] beanDefinitionNmaes=ctx.getBeanDefinitionNames();//获得spring工厂的所有对象的id值
for (String beanDefinitionName: beanDefinitionNmaes){
System.out.println("beanDefinitionName="+beanDefinitionName);
}
}
}
test1
test2:
包含方式是进行扫描 下面的不进行扫描,改为进行 扫描
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--
id:名字唯一
class属性:类的全限定名
通过写bean标签告诉工厂生产的对象
-->
<!-- 让spring框架在设置的包及其子包中扫描对应的注解,使其生效-->
<context:component-scan base-package="com.baizhiedu">
<!--排除特定类型,不进行扫描-->
<context:exclude-filter type="assignable" expression="com.baizhiedu.bean.User"/>
<!--排除特定的注解 不进行扫描 下面排除@Service注解的类-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<!-- aspecj类型 后面可以写包作为切入点还可以类作为切入点
排除特定包及其子包下的类,以包作为切入点:com.baizhidu.injection..*
以类作为切入点:*..User 不管是哪个包里面只要是User类都排除
-->
<context:exclude-filter type="aspectj" expression="com.baizhiedu.injection..*"/>
<context:exclude-filter type="aspectj" expression="*..User"/>
</context:component-scan>
<!-- 包含方式-->
<context:component-scan base-package="com.baizhiedu" use-default-filters="false">
<!-- 只下面写的注解扫描Resposity、Service注解,只会创建UserDAO对象-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<!-- 通过配置文件可以覆注解的内容-->
<bean id="u" class="com.baizhiedu.bean.User">
<property name="id" value="10"/>
</bean>
<!-- 读取配置文件-->
<context:property-placeholder location="classpath:init.properties"/>
<!-- Spring注解与配置文件的互通-->
<bean id="userService" class="com.baizhiedu.injection.UserServiceImpl">
<!--ref可以使用注解产生的对象-->
<property name="userDAO" ref="userDAOImpl"/>
</bean>
</beans>
产生的所有对象:
使用排除方式后:
使用包含方式后: