spring:(二) 配置

一、创建流程

  1. 导入jar包

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.1.10.RELEASE</version>
    </dependency
    
  2. 实体类

    public class Hello {
        private String name;
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    
        public void show(){
            System.out.println("Hello,"+ name );
        }
    }
    
  3. 编写配置文件 , 这里命名为beans.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就是java对象 , 由Spring创建和管理-->
        <bean id="hello" class="com.ry.pojo.Hello">
            <property name="name" value="Spring"/>
        </bean>
    
    </beans>
    
  4. 测试

    @Test
    public void test(){
        //解析beans.xml文件 , 生成管理相应的Bean对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //getBean : 参数即为spring配置文件中bean的id .
        Hello hello = (Hello) context.getBean("hello");
        hello.show();
    }
    
    • 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
    • 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .
    • IOC是一种编程思想,由主动的编程变成被动的接收

修改案例

<?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="MysqlImpl" class="com.kuang.dao.impl.UserDaoMySqlImpl"/>
    <bean id="OracleImpl" class="com.kuang.dao.impl.UserDaoOracleImpl"/>

    <bean id="ServiceImpl" class="com.kuang.service.impl.UserServiceImpl">
        <!--注意: 这里的name并不是属性 , 而是set方法后面的那部分 , 首字母小写-->
        <!--引用另外一个bean , 不是用value 而是用 ref-->
        <property name="userDao" ref="OracleImpl"/>
    </bean>
</beans>
@Test
public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    UserServiceImpl serviceImpl = (UserServiceImpl) context.getBean("ServiceImpl");
    serviceImpl.getUser();
}

对象由Spring 来创建 , 管理 , 装配 !

二、IOC创建对象的方式

通过无参构造方法创建

  1. 实体类

    public class User {
    
        private String name;
    
        public User() {
            System.out.println("user无参构造方法");
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void show(){
            System.out.println("name="+ name );
        }
    
    }
    
  2. 配置文件

    <?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="user" class="com.kuang.pojo.User">
            <property name="name" value="lufei"/>
        </bean>
    
    </beans>
    
  3. 测试

    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //在执行getBean的时候, user已经创建好了 , 通过无参构造
        User user = (User) context.getBean("user");
        //调用对象的方法 .
        user.show();
    }
    

    在调用show方法之前,User对象已经通过无参构造初始化了!

通过有参构造创建

  1. 实体

    public class UserT {
    
        private String name;
    
        public UserT(String name) {
            this.name = name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void show(){
            System.out.println("name="+ name );
        }
    
    }
    
  2. 配置文件(三种方式)

    <!-- 第一种根据index参数下标设置 -->
    <bean id="userT" class="com.ry.pojo.UserT">
        <!-- index指构造方法 , 下标从0开始 -->
        <constructor-arg index="0" value="lufei"/>
    </bean>
    
    <!-- 第二种根据参数名字设置 -->
    <bean id="userT" class="com.ry.pojo.UserT">
        <!-- name指参数名 -->
        <constructor-arg name="name" value="lufei"/>
    </bean>
    
    <!-- 第三种根据参数类型设置 -->
    <bean id="userT" class="com.ry.pojo.UserT">
        <constructor-arg type="java.lang.String" value="lufei"/>
    </bean>
    
  3. 测试

    @Test
    public void testT(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserT user = (UserT) context.getBean("userT");
        user.show();
    }
    

三、别名

<!--设置别名:在获取Bean的时候可以使用别名获取-->
<alias name="userT" alias="userNew"/>

四、Bean配置

<!--
    id 是bean的标识符,要唯一,如果没有配置id,name就是默认标识符
    如果配置id,又配置了name,那么name是别名
    name可以设置多个别名,可以用逗号,分号,空格隔开
    如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象;

    class是bean的全限定名=包名+类名
-->
<bean id="hello" name="hello2 h2,h3;h4" class="com.kuang.pojo.Hello">
    <property name="name" value="Spring"/>
</bean>

五、import

<import resource="{path}/beans.xml"/>

原文地址:https://www.cnblogs.com/dreamzone/p/12360145.html