spring的入门

一、使用spring的五个步骤

目录结构:

1.1、添加spring-context依赖

pom.xml

    <!--第一步:添加spring-context依赖-->
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>

    </dependencies>

1.2、创建xml文件

1.3、配置bean

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

    <!--第二步:创建xml文件-->
    <!--第三步:配置bean-->
    <bean id="stu" class="entity.Student"/>

</beans>

1.4、实例化上下文类

 Student.java

package entity;

/*第四步:实例化上下文类*/
public class Student {
    public Student() {
        System.out.println("student的构造方法");
    }
    public void test(){
        System.out.println("spring的测试");
    }
}

1.5、getBean得到对象

main.java

package entity;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class mian {
    public static void main(String[] args) {
        /*第五步:getBean得到对象*/
        //超级工厂
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Student student = (Student) applicationContext.getBean("stu");
        student.test();
    }
}

二、别名和分隔符

配置bean时,id是唯一的,当然也可以用name指定别名来标记,可写多个别名,可用“,”,“;”,“空格”隔开。注:id和name都是唯一的,不可重复

事例:

    <!--三种分隔符分别是:,;空格-->
    <!--允许使用不同分隔符隔开-->
    <bean id="stu" name="aaa,bbb;ccc eee" class="entity.Student"/>

使用方法和id一样:

package entity;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");

        Student student = (Student) applicationContext.getBean("stu");
        student.test();

        Student aaa = (Student) applicationContext.getBean("aaa");
        aaa.test();

        Student bbb = (Student) applicationContext.getBean("bbb");
        bbb.test();

        Student ccc = (Student) applicationContext.getBean("ccc");
        ccc.test();

        Student eee = (Student) applicationContext.getBean("eee");
        eee.test();
  
    }
}

 三、spring创建对象的四种方法

 方法一:直接创建

spring.xml

<bean id="stu" class="entity.Student"/>

 Main.java

package entity;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");

        Student student = (Student) applicationContext.getBean("stu");
        student.test();

    }
}

 方法二:静态工厂

spring.xml

<!--静态工厂-->
<bean id="entityFactory" class="factory.EntityFactory" factory-method="getStudent"/>

EntityFactory.java

package factory;

import entity.Student;
public class EntityFactory { //静态工厂 public static Student getStudent(){ return new Student(); } }

Main.java

package entity;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");

        Student student = (Student) applicationContext.getBean("entityFactory");
        student.test();
} }

方法三:实例工厂

spring.xml

   <!--实例工厂-->
    <bean name="factory" class="factory.EntityFactory"/>
    <bean name="entityFactory2" class="factory.EntityFactory" factory-bean="factory" factory-method="getTeacher"/>

EntityFactory.java

package factory;

import entity.Student;
import entity.Teacher;

public class EntityFactory {
    //实例工厂
    public Teacher getTeacher(){
        return new Teacher();
    }
}

Main.java

package entity;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");

        Teacher teacher = (Teacher) applicationContext.getBean("entityFactory2");
        teacher.test();

    }
}

方法四:使用FactoryBean创建工厂

StudentFactory.java

package factory;

import entity.Student;
import org.springframework.beans.factory.FactoryBean;

public class StudentFactory implements FactoryBean<Student> {
    public Student getObject() throws Exception {
        return new Student();
    }

    public Class<?> getObjectType() {
        return Student.class;
    }

    public boolean isSingleton() {
        return false;
    }
}

spring.xml

<!--FactoryBean-->
<bean id="studentFactory" class="factory.StudentFactory"/>

Main.java

package entity;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");

        Student student = (Student) applicationContext.getBean("studentFactory");
        student.test();

    }
}

四、初始化与销毁的三种方法 

方法一:直接在bean中定义init-method="方法名"(初始化);destory-method="方法名"(销毁)

<bean id="stu" class="entity.Student" init-method="init" destroy-method="end"/>

方法二:在beans中定义default-init-method=“方法名”(初始化);default-destory-method=“方法名”(销毁)

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd"
       default-init-method="init" default-destroy-method="end"
>

    <bean id="stu" class="entity.Student" />

方法三:在实体类实现InitializingBean(初始化);DisposableBean(销毁)

注:如果在实体类实现初始化、销毁方法与在beans中定义,则先运行在实体类定义的初始化、销毁方法,在实体类中定义的优先级更高。

Student.java

package entity;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class Student implements InitializingBean,DisposableBean {
    
    public void afterPropertiesSet() throws Exception {
        System.out.println("after properties set----");
    }

    public void destroy() throws Exception {
        System.out.println("destory----");
    }
}

spring.xml

<bean id="stu" class="entity.Student" "/> 

五、依赖注入(DI:Dependency inject)(又称:控制反转 Inversion of Control ,缩写:IoC)

依赖注入的种类:

1、 setter方式的注入

Teacher.class

package IoC;

import java.util.List;
import java.util.Map;

public class Teacher {
    private String name;
    private List<String> students;
    private int age;
    private Object aa;
    private Map<String,String> bbb;

    public List<String> getStudents() {
        return students;
    }

    public void setStudents(List<String> students) {
        this.students = students;
    }

    public void info(){
        System.out.println(this.name);
        System.out.println(this.students);
        System.out.println(this.age);
        System.out.println(this.aa);
        System.out.println(this.bbb);
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Object getAa() {
        return aa;
    }

    public void setAa(Object aa) {
        this.aa = aa;
    }

    public Map<String, String> getBbb() {
        return bbb;
    }

    public void setBbb(Map<String, String> bbb) {
        this.bbb = bbb;
    }
}

1.1、给集合设值的三种方式(set、list、props、map)

list:数据可重复

<?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">

    <bean id="tea" class="IoC.Teacher">
        <property name="students">      <!--給集合設值-->
            <!--使用list-->
            <list>
                <value>小張</value>
                <value>小李</value>
                <value>小紅</value>
                <value>小白</value>
            </list>
        </property>
    </bean>
</beans>

set:会去除重复的数据

<?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">

    <bean id="tea" class="IoC.Teacher">
        <property name="students">      <!--給集合設值-->
            <!--使用set-->
            <set>
                <value>小紅</value>
                <value>小紅</value>
                <value>小張</value>
                <value>小里</value>
            </set>
        </property>
    </bean>
</beans>

map:

<?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">


    <bean id="tea" class="IoC.Teacher">
        <property name="bbb">
            <!--使用map給集合設值-->
            <map>
                <entry key="name" value="zss"/>
                <entry key="age" value="18"/>
                <entry key="sex" value="men"/>
            </map>

    </bean>

</beans>

props:

<?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">

    <bean id="tea" class="IoC.Teacher">
        <property name="bbb">
            <!--使用props給集合設值-->
            <props>
                <prop key="name">zss</prop>
                <prop key="age">123</prop>
            </props>
        </property>

    </bean>

</beans>

1.2、直接给字段设值

<property name="age" value="111"> </property>

1.3、设值空值

<property name="aa"><null></null></property>

1.4、内部bean:通过内部的bean来给类的字段设值

A.class

package IoC;

public class A {
    private B b;
    public B getB() {
        return b;
    }
    public void setB(B b) {
        this.b = b;
    }
    public void get(){
        System.out.println(this.b.getName());
    }
}

B.class

package IoC;

public class B {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

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

    <bean class="IoC.A" id="a">
        <property name="b">
            <bean class="IoC.B">
                <property name="name" value="張三三"/>
            </bean>
        </property>
    </bean>

</beans>

2、 构造函数方式的注入

2.1、给构造函数设值(单个值和集合)

Teacher.class

package IoC;

import java.util.List;
import java.util.Map;

public class Teacher {
    private String name;
    private List<String> students;

    public Teacher(String name,List<String> students) {
        this.name = name;
        this.students = students;
    }

    public void info(){
        System.out.println(this.name);
        System.out.println(this.students);
    }

}

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

    <bean id="tea" class="IoC.Teacher">
        <constructor-arg index="0" value="張老師"/>  <!--給構造函數設置-->
        <constructor-arg index="1">     <!--給構造函數賦集合的值-->
            <list>
                <value>小紅</value>
                <value>小小李</value>
                <value>小白</value>
            </list>
        </constructor-arg>
    </bean>

</beans>

 

转载于:https://www.cnblogs.com/huasonglin/p/8287895.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值