spring的介绍
Spring是一个轻量级控制反转和面向切面的容器框架
spring中常见的jar包及其作用?
Spring-aop:提供aop面向切面编程实现
Spring-aspects:spring提供对aspectj框架整合
Spring-beans:提供对ioc的基础实现
Spring-context:spring提供在ioc基础功能上的扩展服务
Spring-context-support对spring-context的扩展支持
Spring-core:核心组件
Spring-expression:spring的核心组件
Spring-jdbc提供对jdbc的支持包
Spring-tx对事务的zhichi
Spring-orm整合disanfang的orm框架
Spring-test:对单元测试的简单封装
Spring-web:springMvc支持web端应用的部署架构
Spring-webmvc:restweb服务和web应用的视图控制器的实现
如何理解Spring的IoC?
控制反转:是一个概念,是一种思想,控制反转就是对对象的控制权的转移,从程序饭吗本身翻转到外部容器,把对象的创建,初始化,销毁等工作交给spring容器来做,由spring容器控制对象的生命周期(其实就是省去了我们创建对象等操作)
简述BeanFactory和ApplicationContext的区别
BeanFactory:创建bean对象稍微晚一些,我们在调用getBean方法时才创建对象,效率低,但是不会占内存
ApplicationContext在容器初始化时所有的bean对象都已经创建,效率高,但是会占用内存
(2)请描述@Autowired和@Resource的区别?
@Autowired:默认是byType方式注入,而如果要通过byName方式注入
必须与@qualifier注解一起使用,是属于spring下注解
@Resource:默认是byName注入,也可以通过byType注入,是属于jdk下注解
常用的注入方式:设值注入 构造注入 ,注解注入
设置注入:基本类型和引用类型注入
<?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">
<!-- 设值方式DI -->
<bean id="star" class="com.bjsxt.pojo.Star">
<property name="name" value="贝克汉姆"></property>
<property name="age" value="39"></property>
<property name="partner" ref="partner"></property>
</bean>
<bean id="partner" class="com.bjsxt.pojo.Partner">
<property name="name" value="维多利亚"></property>
</bean>
</beans>
关于实体类要实现setter和getter方法
package com.bjsxt.pojo;
public class Star {
private String name;
private int age;
private Partner partner;
public Partner getPartner() {
return partner;
}
public void setPartner(Partner partner) {
this.partner = partner;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Star [name=" + name + ", age=" + age + ", partner=" + partner + "]";
}
}
设值注入:集合
<?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">
<!-- 设值方式DI -->
<bean id="partner1" class="com.bjsxt.pojo.Partner">
<property name="name" value="维多利亚"></property>
</bean>
<bean id="partner2" class="com.bjsxt.pojo.Partner">
<property name="name" value="布兰妮"></property>
</bean>
<bean id="someService" class="com.bjsxt.service.SomeService">
<property name="myArray">
<array>
<value>北京</value>
<value>上海</value>
</array>
</property>
<property name="mySet">
<set>
<ref bean="partner1"/>
<ref bean="partner2"/>
</set>
</property>
<property name="myList">
<list>
<value>男</value>
<value>女</value>
</list>
</property>
<property name="myMap">
<map>
<entry key="qq" value="123456"></entry>
<entry key="mobile" value="13300000"></entry>
</map>
</property>
<property name="myProps">
<props>
<prop key="兴趣">足球</prop>
<prop key="爱好">下棋</prop>
</props>
</property>
</bean>
</beans>
实体类
package com.bjsxt.service;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import com.bjsxt.pojo.Partner;
public class SomeService {
private String[] myArray;
private Set<Partner> mySet;
private List<String> myList;
private Map<String, String> myMap;
private Properties myProps;
public String[] getMyArray() {
return myArray;
}
public void setMyArray(String[] myArray) {
this.myArray = myArray;
}
public Set<Partner> getMySet() {
return mySet;
}
public void setMySet(Set<Partner> mySet) {
this.mySet = mySet;
}
public List<String> getMyList() {
return myList;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public Map<String, String> getMyMap() {
return myMap;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public Properties getMyProps() {
return myProps;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
@Override
public String toString() {
return "SomeService [myArray=" + Arrays.toString(myArray) + ", mySet=" + mySet + ", myList=" + myList
+ ", myMap=" + myMap + ", myProps=" + myProps + "]";
}
}
设值注入:域属性自动注入
<?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"
default-autowire="byType">
<!-- byName方式域属性自动注入:要求注入的bean的id必须和被注入的bean对象的属性名一致 -->
<!--
<bean id="star" class="com.bjsxt.pojo.Star" autowire="byName">
<property name="name" value="贝克汉姆"></property>
<property name="age" value="39"></property>
</bean>
-->
<!-- byType方式域属性自动注入:spring配置文件中查询与属性类型一致的bean并进行注入 -->
<!--
<bean id="star" class="com.bjsxt.pojo.Star" autowire="byType">
<property name="name" value="贝克汉姆"></property>
<property name="age" value="39"></property>
</bean>
-->
<bean id="star" class="com.bjsxt.pojo.Star">
<property name="name" value="贝克汉姆"></property>
<property name="age" value="39"></property>
</bean>
<bean id="partner" class="com.bjsxt.pojo.Partner">
<property name="name" value="维多利亚"></property>
</bean>
</beans>
设置注入:null 空字符
<?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">
<!-- 设值方式DI -->
<bean id="star" class="com.bjsxt.pojo.Star">
<!-- 给属性注入空字符串:通过<value/> -->
<!-- <property name="name"><value/></property> -->
<!-- 给属性注入null值:通过<null/> -->
<property name="name"><null/></property>
<property name="age" value="39"></property>
<property name="partner" ref="partner"></property>
</bean>
<bean id="partner" class="com.bjsxt.pojo.Partner">
<property name="name" value="维多利亚"></property>
</bean>
</beans>