spring overview
实现
实体类定义
@Bean
public class Blog {
private Integer id;
private String title;
private String[] comments;
private Person author;
private List<Person> follows;
private Map<String, String> changes;
private Set<Person> like;
private Properties info;
}
bean 配置
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- more bean definitions go here -->
<bean id="person" class="cn.spring.pojo.Person">
<property name="name" value="张三"/>
<property name="id" value="1"/>
</bean>
<bean id="blog" class="cn.spring.pojo.Blog">
<!-- 第一种注入,普通值注入, 使用value属性-->
<property name="title" value="a blog"/>
<!-- 第二种注入,bean注入,使用ref属性-->
<!-- <property name="author" ref="person"/>-->
<property name="author">
<bean class="cn.spring.pojo.Person">
<property name="id" value="2"/>
<property name="name" value="李四"/>
</bean>
</property>
<!-- 第三种注入,数组注入,使用array value标签组-->
<property name="comments">
<array>
<value>this is a comment</value>
<value>this is a comment</value>
<value>this is a comment</value>
<value>this is a comment</value>
</array>
</property>
<!-- 第四中注入,列表注入,使用list标签 其中嵌套bean标签或者value标签-->
<property name="follows">
<list>
<bean id="person1" class="cn.spring.pojo.Person">
<property name="id" value="3"/>
<property name="name" value="王五"/>
</bean>
<bean id="person2" class="cn.spring.pojo.Person">
<property name="id" value="4"/>
<property name="name" value="田六"/>
</bean>
</list>
</property>
<!-- 第五种注入,map注入,使用map entry标签对-->
<property name="changes">
<map>
<entry key="第一次改动" value="改动内容"/>
<entry key="第二次改动" value="改动内容"/>
</map>
</property>
<!-- 第六种注入,set注入, 使用set标签-->
<property name="like">
<set>
<ref bean="person"/>
</set>
</property>
<!-- 第七种注入,空值注入,一种使用null标签,第二种将value值设为空串就行-->
<!-- <property name="id">-->
<!-- <null/>-->
<!-- </property>-->
<property name="id" value=""/>
<!-- 第八中输入,property注入,使用proos 和prop标签对-->
<property name="info">
<props>
<prop key="link">www.baidu.com</prop>
</props>
</property>
</bean>
</beans>
结果
@Test
public void iocTest() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Blog blog = applicationContext.getBean(Blog.class);
/*
* Blog
* (id=null,
* title=a blog,
* comments=[this is a comment, this is a comment, this is a comment, this is a comment],
* author=Person(id=2, name=李四),
* follows=[Person(id=3, name=王五), Person(id=4, name=田六)],
* changes={第一次改动=改动内容, 第二次改动=改动内容},
* like=[Person(id=1, name=张三)],
* info={link=www.baidu.com})
* */
System.out.println(blog);
}
注意点
bean注入
使用bean注入可以使用两种方法
- 一是使用自闭和标签用ref属性来指定一个定义过的bean
<property name="author" ref="person"/>
- 第二种就是使用内嵌的bean子标签,值得注意的是,此时定义的这个bean并不会能被其他bean所调用,类似于java中匿名内部类(就算你在这个bean标签内使用id和name属性来命名,在外部也是无法检测到的)至于它是实现方法是不是就是使用java中的匿名内部类,由于没看过spring源码就不去猜测了。
set list array 和 map 下的entry
这几个标签下都可以嵌套根据你在实体类中的定义嵌套使用 value, bean, ref, idref, props, null以及他们自生这几个标签,总之就是对应java类进行出力
空值注入
控制注入也有两种方式,但是这两种方式所得到的结果是不一样的!
- 一是使用
<null/>标签 - 而是是value的值为空字符串,即:
<property name="id" value=""/>
他们两个在String类型下得到结果自然是一一对应的""和null。而如果为其他java基本数据类型的话两个都为null。如果为其他类型使用空串进行注入则会报错。其中byte类两种方式都会报错,暂时还不清楚它具体的判断逻辑。
spring注解开发
使用注解配置
@Configuration 使用该注解是一个类变成一个spring配置类。该注解本身也是一个Component容器,会将类扫描到spring容器中。
- 配合的注解:
-
@ComponentScan指定扫描的包名 -
@Bean注册beans -
@Import合并多个配置类
-
扫描
``
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="xxx"/>
</beans>
自动注入
@Autowried
-
@Autowired可以在属性上使用,也可以在set方法上使用。
可以配合required属性或者@Nullable注解使用使得这个对象可以为null - 配合
@Qualifier(value="bean")使用可以指定哪一个具体的bean作为注入对象 -
@Resource(name="bean")的效果类似于@Autowired的效果,但是@Autowired是byType选择,配合@Qualifier可以做到byName。而@Resource(name="bean")默认byName,如果找不到,还会继续用byType进行查找。
@Component
@Component 该注解下的类都会被扫描到Ioc容器中配合@Value可以对bean进行配置,与之有相同功能的还有@Service @Controller @Repository他们都是@Component的别名,有这一项的效果,一般用于帮助程序员区分各个层级。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {
String value() default "";
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
@Scope
指定bean的作用域,有prototype, singleton,request,session,application, websocket 这六种(后四种为webAppliction专属)。 默认为单例模式(singleton)