Spring
SpringIOC
Spring的配置
1.spring2.5前==xml
2.spring2.5后==xml+annotation
3.spring3.0后==JavaConfig+annotation配置类
下方为示例配置类
@Configuration
@ComponentScan(basePackages="com.apesource")
@PropertySource(value="stu.properties")
@Import(DataSourceConfig.class)
public class AnnotationConfig {
// @Bean
// public IUserDao getDao(){
// return new UserDao();
// }
// @Bean
// public IUserService getService(IUserDao userDao){
// return new UserService(userDao);
// }
// @Bean
// public IUserController getController(IUserService service){
// return new UserController(service);
// }
@Bean(name="time")
public Date getDate(){
return new Date();
}
}
Bean的实例化
- 默认通过无参构造
- 普通工厂实例化
- 静态工厂实例化
Bean的作用域
singleton :Spring 只会为该 bean 对象只会创建唯一实例,Spring中的bean默认都是单例;
prototype:每次获取 bean,Spring 会创建一个新的 bean 实例;
request:每一次Http请求,Spring会创建一个新的 bean 实例;
session:不同的 HTTP 会话,Spring会创建不同的bean 实例;
通过XML方式设置bean的作用域
<bean id="demoDaoBean" class="com.apesource.dao.DemoDA0Impl" scope="singleton"/>
通过注解方式设置bean的作用域
@Scope(ConfigurableBeanFactory.SCOPE PROTOTYPE)
public class DemoDAOImpl implements IDemoDAo{}
Spring DI
DI(Dependecy Inject,中文释义:依赖注入)是对IOC概念的不同角度的描述,是指应用程序在运行时,每一个bean对象都依赖IOC容器注入当前bean对象所需要的另外一个bean对象。(例如在MyBatis整合Spring时,SqlSessionFactoryBean 依赖IOC容器注入一个DataSource数据源bean)。
Spring DI 的实现方式
1. set注入
在<bean>标签中使用<property>标签注入
1.1 首先创建实体类,并重写属性的set方法。
public class Student {
private String stuName;
private String hobby;
private int age;
public void setStuName(String stuName) {
this.stuName = stuName;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public void setAge(int age) {
this.age = age;
}
}
1.2 在Spring配置文件中配置
<bean id="student" class="com.pojo.Student">
<property name="stuName" value="wwt">
</bean>
2. 构造注入
在<bean>标签中使用<constructor-arg>标签注入
2.1首先在实体类实现有参无参构造
public class Student {
private String stuName;
private String hobby;
private int age;
public Student(String stuName, String hobby, int age) {
this.stuName = stuName;
this.hobby = hobby;
this.age = age;
}
public Student() {
}
public Student(String stuName, String hobby) {
this.stuName = stuName;
this.hobby = hobby;
}
}
2.2在Spring配置文件中配置
<bean id="student" class="com.pojo.Student">
<constructor-arg index="0" value="川普"></constructor-arg>
<constructor-arg index="1" value="演讲"></constructor-arg>
</bean>
3. 注解注入
3.1举例
@Component
public class Student {
@Value("wwt")
private String stuName;
@Value("game")
private String hobby;
@Value("$21")
private int age;
}
支持的数据类型
- 基本类型与String
举例同上方Set注入
- javaBean对象
(1)在配置文件中注入
2.1.1 如下Techer类中student属性为另一实体类
public class Teacher {
private Student student;
public Teacher(Student student) {
this.student = student;
}
public Teacher() {
}
}
2.1.2 先配置student实体类再配置teacher实体类
此处使用构造注入<constructor-arg>标签中的ref属性用来引入已经实例化的bean
<bean id="student" class="com.pojo.Student">
<constructor-arg name="stuName" value="wwt"></constructor-arg>
<constructor-arg name="hobby" value="youxi"></constructor-arg>
</bean>
<bean id="teacher" class="com.pojo.Teacher">
<constructor-arg name="student" ref="student"></constructor-arg>
</bean>
(2)使用注解注入
需要在注入和被注入类上添加注入Ioc容器类型注入如:@Component、@Service等等,在注入的类中声明被注入对象,在其上方添加@Autowired标签,该标签要与<context:component-scan base-package=“com”></context:component-scan>搭配使用,该注解用于扫描包路径。
@Component
public class UserService implements IUserService{
@Autowired
IUserDao userDao;
@Override
public void save() {
System.out.println("service层的save方法");
userDao.save();
}
}
- 复杂类型
3.1定义一个属性全为复杂类型的Teacher类
public class Teacher {
private List tName;
private Map tHobby;
private String[] tId;
private Properties tF;
private Set tL;
public void settL(Set tL) {
this.tL = tL;
}
public void settName(List tName) {
this.tName = tName;
}
public void settHobby(Map tHobby) {
this.tHobby = tHobby;
}
public void settId(String[] tId) {
this.tId = tId;
}
public void settF(Properties tF) {
this.tF = tF;
}
public Teacher(List tName, Map tHobby, String[] tId, Properties tF, Set tL) {
this.tName = tName;
this.tHobby = tHobby;
this.tId = tId;
this.tF = tF;
this.tL = tL;
}
public Teacher() {
}
@Override
public String toString() {
return "Teacher{" +
"tName=" + tName +
", tHobby=" + tHobby +
", tId=" + Arrays.toString(tId) +
", tF=" + tF +
", tL=" + tL +
'}';
}
}
3.2在spring配置文件中通过set注入
<bean id="teacher" class="com.apesource.pojo.Teacher">
<property name="tName">
<list>
<value>张三</value>
<value>李四</value>
<value>王二麻子</value>
</list>
</property>
<property name="tHobby">
<map>
<entry key="user" value="root"></entry>
<entry key="pwd" value="489123"></entry>
</map>
</property>
<property name="tId">
<array>
<value>S0123</value>
<value>S0124</value>
<value>S0125</value>
<value>S0126</value>
</array>
</property>
<property name="tF">
<props>
<prop key="youxi">LoL</prop>
<prop key="showyou">PS</prop>
<prop key="duanyou">CS</prop>
</props>
</property>
<property name="tL">
<set>
<value>wwt</value>
</set>
</property>
</bean>
- List类型:<list><value>值</value></list>
- Map类型:<map><entry key=“a” value=b"></entry></map>
- 数组类型:<array><value>值</value></array>
- Set类型:<set><value>值</value></set>
- Properties类型:<props><prop key=“a”>值</prop></props>
注解
1.注入类
替换:<bean id=“” class=“”></bean>
位置:类
语法:@Component(value=“注入容器中的id,如果省略id为类名且首字母小写,value属性名称可以省略”)
@Component
eg:Class User{}
<bean id=“user” class=“com.apesource.包.User”></bean>
||等价于||
@Component
Class User{}
<context:component-scan base-package=“”></context:component-scan>功能一致
含义:扫描所有被@Component注解所修饰的类,注入容器
@Repository=====>注入数据访问层
@Service========>注入业务层
@Controller=====>注入控制层
以上三个注解与@Component功能语法一致
2.注入基本数据,javaBean
@Value
含义:注入基本数据
替换:<property></property>
修饰:成员变量或对应的set方法
语法:@Value(“数据内容”)
@Value(“${动态获取}”)
加载指定资源文件:<context:property-placeholder location=“classpath:jdbc.properties”></context:property-placeholder>
@Autowired
替换:DI实现javaBean注入
语法:@Autowired(required = “true-默认、false、是否必须进行装配”)
修饰:成员变量或对应的构造方法
含义:按照通过构造方法进行“类型装配”,构造方法可以省略
注意:
1.默认是按照类型装配且同构造方法
2.若容器中有一个类型可以与之匹配则装配成功,若没有一个类型可以匹配则报错NoSuchBeanDefinitionException
3.若容器中有多个类型可以与之匹配,则自动切换为按照名称装配,若名称没有对应,则报错NoUniqueBeanDefinitionException
3.其他注解
@Primary
含义:首选项,当类型冲突的情况下,此注解修饰的类被列为首选(备胎扶正)
修饰:类
注意:不能单独使用,必须与@Component…联合使用
@Qualifier(value=“名称”)
含义:按照名称装配
修饰:成员变量
注意:不能单独使用,必须与@Autowired联合使用
@Resource(name=“名称”)
含义:按照名称装配
修饰:成员变量
注意:单独使用
@Scope
含义:配置类的作用域
修饰:类
注意:不能单独使用,必须与@Component…联合使用
@Scope(“prototype”)
@Scope(“singleton”)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
@PostConstruct:初始化,修饰方法 替换:init-method
@PreDestroy:销毁,修饰方法 替换:destory-method
spring中的新注解
@Configuration
作用:指定当前类是一个配置类,替代applicationContext.xml文件
细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。
@ComponentScan
作用:用于通过注解指定spring在创建容器时要扫描的包
替换:<context:component-scan base-package=“com.apesource”></context:component-scan>
@PropertySource
作用:用于指定properties文件的位置
属性:value:指定文件的名称和路径。
替换:<context:property-placeholder location=“classpath:jdbc.properties”></context:property-placeholder>
@Bean
作用:用于把当前方法的返回值作为bean对象存入spring的容器中
属性:name:用于指定bean的id。当不写时,默认值是当前方法的名称
@Import
作用:用于导入其他的配置类
属性:value:用于指定其他配置类的字节码。
例子:@Import(SystemSpringConfig.class)