2.Spring第一个程序、对象创建方式、配置说明
转载:https://blog.kuangstudy.com/index.php/archives/518/
一.Spring的第一个程序
1.开发步骤
-
导包
-
实体类
-
配置文件
-
测试类
(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 }
(5)说明
-
Spring配置方式有两种:
-
使用xml配置:官网创建ApplicationContext.xml,此处我们可以简化名字创建beans.xml
-
使用注解配置
-
-
标签:
-
<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.对象创建方式有两种
-
无参构造函数创建
-
有参构造函数创建(3种)
-
根据index参数下标设置
-
根据参数名字设置(推荐使用)
-
根据参数类型设置(不推荐使用,参数类型可能会重复,参数类型可能会写错)
-
(1)无参构造函数
User.java
1 package ustc.wzh.pojo; 2 3 public class User { 4 5 private String name; 6 7 public User() { 8 System.out.println("user无参构造方法"); 9 } 10 11 public void setName(String name) { 12 this.name = name; 13 } 14 15 public void show(){ 16 System.out.println("name="+ name ); 17 } 18 19 }
beans.xml
<bean id="user" class="ustc.wzh.pojo.User">
<property name="name" value="无参构造用户"></property>
</bean>
(2)有参构造函数
<!-- 第一种根据index参数下标设置 --> <bean id="userT" class="ustc.wzh.pojo.UserT"> <!-- index指构造方法 , 下标从0开始 --> <constructor-arg index="0" value="有参索引构造用户"/> </bean> <!-- 第二种根据参数名字设置 --> <bean id="userT" class="ustc.wzh.pojo.UserT"> <!-- name指参数名 --> <constructor-arg name="name" value="有参属性名字构造用户"/> </bean> <!-- 第三种根据参数类型设置 --> <bean id="userT" class="ustc.wzh.pojo.UserT"> <constructor-arg type="java.lang.String" value="有参参数类型构造用户"/> </bean>
三.Spring 配置
1.alias别名:
-
name为已有对象,alias为已有对象的别名
<!--设置别名:在获取Bean的时候可以使用别名获取--> <alias name="userT" alias="userNew"/>
2.Bean配置
-
name可以设置多个别名,可以用逗号,分号,空格隔开
<!--bean就是java对象,由Spring创建和管理--> <!-- 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>
3.import
-
用于团队开发来导入别的beans.xml配置文件
<import resource="{path}/beans1.xml"/> <import resource="{path}/beans2.xml"/> <import resource="{path}/beans3.xml"/>