一、为什么要使用Spring,什么是Bean,他有什么作用?

Spring,主要围绕AOP和IOC,这里面的知识有是一堆,所以这里不写了嘻嘻,可以理解为他是一个Bean工厂或者一个对象容器,管理里所有的对象,我这里

只管如何进行Bean的注入,其他的之后后面再讲;



二、Bean的注入

例如有类:Persion

Package com.persion 
 
//class Student 
 
public class Student { 
 
        private String name; 
 
        private Integer age; 
 
        /*getset*/ 
 
}

1、使用getter/setter注入

条件:该类的属性需要实现get和set方法;

<!--propertyvalueref-->
<bean id=”student ” class=”com.persion.Student”>
    <property name="name" value="zhansan"/>
    <property name="age" value=12/>
</bean>

2、通过构造器注入

条件:该类中必须有对应的构造方法存在(可以没有getter/setter方法);

<!--constructor-arg-->
<!--constructor-arg-->
<!--index:0-->
<!--name-->
<!--type-->
<bean id="student " class="com.persion.Student">
    <!--constuctorPerson-->
    <constructor-arg value=""></constructor-arg>
    <constructor-arg value="20"></constructor-arg>
</bean>

 3、通过静态工厂

条件:需要有对应的工厂类并实现了创建对应类的静态方法

public class PersonBeanFactory { 
 public static Student createStudentBean(){ 
     return new Student(); 
 } 
 public static Teacher createTeacherBean(){ 
     return new Teacher(); 
 } 
}
<!--constructor-arg-->
<bean id="student" class="com.persion.PersonBeanFactory" factory-method="createStudentBean">
    <constructor-arg value=""></constructor-arg>
    <constructor-arg value="20"></constructor-arg>
</bean>

4、实例工厂

public class PersonBeanFactory {
    public Student createStudentBean(){
        return new Student();
    }
    public Teacher createTeacherBean(){
        return new Teacher();
    }
}
<!--constructor-arg-->
<bean id="student" class="com.persion.PersonBeanFactory" factory-bean="PersonBeanFactory" factory-method="
createStudentBean">
    <constructor-arg value=""></constructor-arg>
    <constructor-arg value="20"></constructor-arg>
</bean>

5、使用注解自动扫描注入(这里不详细介绍,详见springboot部分文档)

注解相当于使用xml配置方式注入Bean,在添加了注解并进行路径配置后,spring会在启动后自动扫描路径下的所有注解,自动进行Bean注入

注意: 默认不会自动扫描,我们需要配置

@Component("person") //
@Service("person") //Service
@Repository("person") //
@Controller("person") //Controller
@Scope(scopeName="singleton")//
@Scope(scopeName="prototype")//

配置 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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="com.persion"></context:component-scan>

</beans>

在启动类加载配置类

//@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

6、注解配置注入

我们也可以使用注解去代替xml配置(使用注解+java代码实现配置文件的功能)

//xmlbeanbean
@Configuration
public class Persion {
    @Bean
    public Student createStudent() {
        return new Student();
    }
    @Bean
    public Teacher createTeacher() {
        return new Teacher();
    }
}

默认不启用组件扫描,我们需要配置下(表示在Spring中开启了组件扫描,默认扫描与配置类相同的包):

@Configuration
@ComponentScan
public class ScanConfig {

}

7、命名空间注入

条件:要求类必须有getter/setter方法

使用:

在spring配置文件中,添加命名空间:

xmlns:p=" http://www.springframework.org/schema/p "

在bean中,通过p:属性的方式来完成参数的注入

如果注入的是单个值,那么注入格式为:p:属性名="值"

如果注入的是对象,那么注入格式为:p:属性名-ref="对象名"

三、Bean的引用(基于BeanFactory,ApplicationContext,注解)

BeanFactory不很常用(框架本身的加载机制),常用ApplicationContext(区别)

例:

public static void main(String []args)throws Throwable{
    ApplicationContext ctx=new ClassPathXmlApplicationContext("Beans.xml");
    Student stu = ctx.getBean("student",Student.class);
    System.out.println(stu);
}

ApplicationContext:

ApplicationContext常用实现类 作用

AnnotationConfigApplicationContext 

从一个或多个基于java的配置类中加载上下文定义,适用于java注解的方式

ClassPathXmlApplicationContext 

从类路径下的一个或多个xml配置文件中加载上下文定义,适用于xml配置的方式

FileSystemXmlApplicationContext 

从文件系统下的一个或多个xml配置文件中加载上下文定义,也就是说从系统盘符中加载xml配置文件

AnnotationConfigWebApplicationContext

专门为web应用准备的,适用于注解方式

XmlWebApplicationContext 

从web应用下的一个或多个xml配置文件加载上下文定义,适用于xml配置方式 四、Bean的作用域(在Spring中主要用到两个:Singleton(默认)和Prototype。)

Bean的两个作用类型的区别:

1、Singleton:

对于每个Spring Ioc容器而言,每个bean定义只有一个对象实例,这同Java对象中的Singleton不一样。在Spring中,singleton是基于Spring Ioc容器这个

Level,而java中是针对与JVM这个Level。默认情况下,系统会在容器启动的时候加载,即在容器加载完成后去调用该Bean的默认构造函数。当然,可以在

bean.xml中配置延迟加载(Lazy-init=”true”),或者是要求所有的bean都使用延迟加载(default-lazy-init=”true”).

2. Prototype:

对于Prototype而言,每次都会创建一个新的对象实例。而Bean的实例化是在调用getBean方法时

Bean的生命周期:

1、Bean实例化

Bean的默认构造函数(如果我们写了构造函数,那么会调用我们的构造函数)

2、Bean的初始化

Init()方法中可以进行初始化。

3、Bean的使用

getBean()方法可以获取当前的Bean,从而做相对应的业务操作。

4、Bean的销毁

destroy()方法执行bean的销毁。 

详细的xml配置 :

<bean id="student" class="com.persion.Student"
scope="singleton" lazy-init="true" init-method="init" destroy-method="destory"
autowire="autodetect" dependency-check="simple"/>

Autowire:对应有五个Value

No

不使用自动装配

byname

根据属性名自动装配。

byType

根据属性类型自动装配

Construct

or

与byType方式类似,不同之处在于它应用于构造器参数。如果容器中没有找到与构造器参数类型一致的bean,那么抛出异常

Autodetect

通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果没有发现默认的构造器,那么将使

用byType方式

Dependency-check属性是同autowire属性一同使用,就是说只有在使用自动装载的时候,才会发挥作用。主要有四个Value:

Simple

对基本类型,字符型和集合进行依赖检查

Object

对依赖的对象进行检查

All

对全部属性进行检查

None

不进行依赖检查(默认)