2.Spring第一个程序、对象创建方式、配置说明

转载:https://blog.kuangstudy.com/index.php/archives/518/

一.Spring的第一个程序

1.开发步骤

  1. 导包

  2. 实体类

  3. 配置文件

  4. 测试类

(1)导包

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.0.RELEASE</version>
</dependency>

(2)实体类

 1 package ustc.wzh.pojo;
 2 
 3 public class Hello {
 4 
 5     private String str;
 6 
 7     public String getStr() {
 8         return str;
 9     }
10 
11     public void setStr(String str) {
12         this.str = str;
13     }
14 
15     @Override
16     public String toString() {
17         return "Hello{" +
18                 "str='" + str + '\'' +
19                 '}';
20     }
21 }

(3)配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         https://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7 
 8     <!--使用Spring来创建对象,在Spring这些都称为Bean
 9 
10         id:变量名
11         class:所属类名
12         property:对象中的属性赋值
13 
14         注:在实体类中必须有set方法才能给属性赋值
15     -->
16     <bean id="hello" class="ustc.wzh.pojo.Hello">
17         <property name="str" value="你好"></property>
18     </bean>
19 
20 
21 </beans>

(4)测试类

 1 import org.springframework.context.ApplicationContext;
 2 import org.springframework.context.support.ClassPathXmlApplicationContext;
 3 import ustc.wzh.pojo.Hello;
 4 
 5 public class MyTest {
 6 
 7     public static void main(String[] args) {
 8         //获取Spring的上下文对象
 9         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
10 
11         //Spring管理对象,直接取出来就可以使用
12         Hello h = (Hello) context.getBean("hello");
13         System.out.println(h.toString());
14     }
15 }

(2)说明

  • Spring配置方式有两种:

    1. 使用xml配置:官网创建ApplicationContext.xml,此处我们可以简化名字创建beans.xml

    2. 使用注解配置

  • 标签:

    • <bean>的id配置变量名,class配置类名

    • <property>的name配置属性名,value配置基本类型的值(还有ref为用于引用Spring容器创建好了的对象)

2.修改spring-01-ioc1程序

配置文件beans.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         https://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7 
 8     <!--使用Spring来创建对象,在Spring这些都称为Bean
 9 
10         id:变量名
11         class:所属类名
12         property:对象中的属性赋值
13 
14         注:在实体类中必须有set方法才能给属性赋值
15     -->
16     <bean id="mysqlImpl" class="ustc.wzh.dao.UserDaoMySqlImpl"></bean>
17     <bean id="oracleImpl" class="ustc.wzh.dao.UserDaoOracleImpl"></bean>
18     <bean id="userServiceImpl" class="ustc.wzh.service.UserServiceImpl">
19         <!--
20             ref:用于引用Spring容器创建好了的对象
21             value:具体的值,用于基本类型
22         -->
23         <property name="userDao" ref="mysqlImpl"></property>
24     </bean>
25 
26 
27 </beans>

测试程序

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ustc.wzh.service.UserServiceImpl;

public class MySpringTest {

    public static void main(String[] args) {

        //获取容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        //获取对象
        UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl");

        //使用对象
        userServiceImpl.getUser();

    }
}

二.IOC对象创建方式

1.对象创建方式有两种

  1. 无参构造函数创建

  2. 有参构造函数创建(3种)

    1. 根据index参数下标设置

    2. 根据参数名字设置(推荐使用)

    3. 根据参数类型设置(不推荐使用,参数类型可能会重复,参数类型可能会写错)

 

猜你喜欢

转载自www.cnblogs.com/zhihaospace/p/12323165.html