java_spring:day02

快速入门:

pom文件添加依赖jar包:
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-coreartifactId>
  <version>5.2.7.RELEASEversion>
dependency>

<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-contextartifactId>
  <version>5.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-beansartifactId>
  <version>5.2.7.RELEASEversion>
dependency>

<dependency>
  <groupId>org.springframeworkgroupId>
  <artifactId>spring-expressionartifactId>
  <version>5.2.7.RELEASEversion>
dependency>
<dependency>
  <groupId>commons-logginggroupId>
  <artifactId>commons-loggingartifactId>
  <version>1.2version>
dependency>
写一个UserService接口
public interface UserService {

    public void add();
}
写一个类实现UserService接口
public class UserServiceImpl implements UserService {

    public void add() {
        System.out.println("增加用户。。。。。");
    }
}
beans文件

<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="userService" class="com.hetl.day01.impl.UserServiceImpl">bean>

beans>
测试
@Test
public void test01(){
        /*
        //以前使用UserService的方式
        UserService u = new UserServiceImpl();
        u.add();

         */

        //从spring容器中获取UserService
        //加载beans.xml文件 内部就会创建对象
        ApplicationContext c = new ClassPathXmlApplicationContext("beans.xml");
        //从spring容器获取userService对象

        UserService u1 = (UserService) c.getBean("userService");
        u1.add();

        UserService u2 = (UserService) c.getBean("userService");
        u2.add();

        System.out.println(u1);
        System.out.println(u2);

    }
输出:
增加用户。。。。。
增加用户。。。。。
com.hetl.day01.impl.UserServiceImpl@58c1670b
com.hetl.day01.impl.UserServiceImpl@58c1670b

u1与u2为同一个值。

依赖注入:

为UserServiceImpl配置get、set方法
public class UserServiceImpl implements UserService {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public void add() {
        System.out.println("增加用户。。。。。"+name);
    }
}
在beans.xml进行注入

<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="userService" class="com.hetl.day01.impl.UserServiceImpl">
        
        <property name="name" value="李四"/>
    bean>

beans>
测试输出
增加用户。。。。。李四
增加用户。。。。。李四

spring容器加载beans.xml的三种方式

第一种 ClassPathXmlApplicationContext(最常用)
第二种 文件系统路径获取配置文件
第三种 使用BeanFactory

    @Test
    public void test02(){
        //spring容器加载的三种方式
        //第一种 ClassPathXmlApplicationContext
        ApplicationContext c = new ClassPathXmlApplicationContext("beans.xml");
        UserService u = (UserService) c.getBean("userService");
        u.add();

        //第二种 文件系统路径获取配置文件
        ApplicationContext c1 = new FileSystemXmlApplicationContext("D:\\along_he\\spring_study\\src\\main\\resources\\beans.xml");
        UserService u1 = (UserService) c1.getBean("userService");
        u1.add();

        //第三种 使用BeanFactory
        BeanFactory bf  = new XmlBeanFactory(new FileSystemResource("D:\\along_he\\spring_study\\src\\main\\resources\\beans.xml"));
        UserService u2 = (UserService) bf.getBean("userService");
        u2.add();

    }

BeanFactory与ApplicationContext的区别

BeanFactory,第一次getBean时才会初始化Bean,采用了延迟加载的方式
ApplicationContext是BeanFactory的扩展,提供了更多的服务功能,及时加载。

装配Bean

1.在Beans.xml文件中写一个

<bean id="userService" class="com.hetl.day01.impl.UserServiceImpl">
        
        <property name="name" value="李四"/>
    bean>

2.通过静态工厂

<bean id="userService1" class="com.hetl.day01.factory.UserServiceFactory" factory-method="creatUserService">
        
    bean>
public class UserServiceFactory {

    public static UserService creatUserService(){
        return new UserServiceImpl();
    }
}

    @Test
    public void test03(){
        ApplicationContext c = new ClassPathXmlApplicationContext("beans.xml");
        UserService u = (UserService) c.getBean("userService1");
        u.add()

    }

3.通过实例工厂的方法

<bean id="factory" class="com.hetl.day01.factory.UserServiceFactory" />
    <bean id="userService2" factory-bean="factory" factory-method="creatUserService">bean>
public class UserServiceFactory {

    public  UserService creatUserService(){
        return new UserServiceImpl();
    }
}



    @Test
    public void test03(){
        ApplicationContext c = new ClassPathXmlApplicationContext("beans.xml");
        UserService u = (UserService) c.getBean("userService2");
        u.add()

    }

你可能感兴趣的:(spring,spring,java)