Spring的基本使用

Spring的基本使用

一、使用

1、导入Jar包

: spring 需要导入commons-logging进行日志记录 .

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

2、编写一个Hello实体类

public class Hello {
   private String name;
	//省略了get和set方法
   public void show(){
       System.out.println("Hello,"+ name );
  }
}

3、编写我们的spring文件 , 这里我们命名为applicationContext.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="cn.domain.Hello">
       <property name="name" value="Spring"/>
   </bean>
<bean id="ServiceImpl" class="cn.service.UserServiceImpl">
       <!--注意: 这里的name并不是属性 , 而是set方法后面的那部分 , 首字母小写-->
       <!--引用另外一个bean , 不是用value 而是用 ref-->
       <property name="userDao" ref="OracleImpl"/>
   </bean>

</beans>

4、我们可以去进行测试了 .

@Test
public void test(){
   //解析applicationContext.xml文件 , 生成管理相应的Bean对象
   ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
   //可以读多个文件这个方法
   ApplicationContext context2 = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
   //getBean : 参数即为spring配置文件中bean的id .
   //第一种写法
   Hello hello = (Hello) context.getBean("hello");
   Hello hello2 = context.getBean("hello",Hello.class);
   hello.show();
}

说明一下:
hello 对象是由Spring创建的, hello 对象的属性是由Spring容器设置,这个过程就叫控制反转

  1. 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
  2. 反转 : 程序本身不创建对象 , 而变成被动的接收对象 . 依赖注入 : 就是利用set方法来进行注入的.
  3. IOC是一种编程思想,由主动的编程变成被动的接收

二、IOC创建对象方式

通过无参构造方法来创建

public class Hello {
   private String name;
    public Hello() {
       System.out.println("Hello无参构造方法");
  }

    public void setName(String name) {
       this.name = name;
  }
	//省略了get
	  public void show(){
       System.out.println("Hello,"+ name );
  }
}

3、编写我们的spring文件 , 这里我们命名为applicationContext.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="cn.domain.Hello">
       <property name="name" value="Spring"/>
   </bean>

</beans>
@Test
public void test(){
   //解析applicationContext.xml文件 , 生成管理相应的Bean对象
   ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
   //可以读多个文件这个方法
   ApplicationContext context2 = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
   //getBean : 参数即为spring配置文件中bean的id .
   //第一种写法
   Hello hello = (Hello) context.getBean("hello");
   Hello hello2 = context.getBean("hello",Hello.class);
   hello.show();
}

结果可以发现,在调用show方法之前,Hello对象已经通过无参构造初始化了!

通过有参构造方法来创建

public class Hello {
   private String name;
    public Hello(String name) {
        this.name = name;
  }

    public void setName(String name) {
       this.name = name;
  }
	//省略了get
	  public void show(){
       System.out.println("Hello,"+ name );
  }
}

3、编写我们的spring文件 , 这里我们命名为applicationContext.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">
       
	   <!-- 第一种根据index参数下标设置 -->
	  <bean id="hello" class="cn.domain.Hello">
	   <!-- index指构造方法 , 下标从0开始 -->
	   <constructor-arg index="0" value="Spring"/>
	</bean>
	
		<!-- 第二种根据参数名字设置 -->
	 <bean id="hello" class="cn.domain.Hello">
	   <!-- name指参数名 -->
	   <constructor-arg name="name" value="Spring"/>
	</bean>
	<!-- 第三种根据参数类型设置 -->
	 <bean id="hello" class="cn.domain.Hello">
	   <constructor-arg type="java.lang.String" value="Spring"/>
	</bean>

</beans>

测试

@Test
public void test(){
   //解析applicationContext.xml文件 , 生成管理相应的Bean对象
   ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
   //可以读多个文件这个方法
   ApplicationContext context2 = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
   //getBean : 参数即为spring配置文件中bean的id .
   //第一种写法
   Hello hello = (Hello) context.getBean("hello");
   Hello hello2 = context.getBean("hello",Hello.class);
   hello.show();
}

在配置文件加载的时候。其中管理的对象都已经初始化了!

三、Spring配置

别名

alias 设置别名 , 为bean设置别名 , 可以设置多个别名

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

Bean的配置

<!--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="cn.domain.Hello">
   <property name="name" value="Spring"/>
</bean>

import

使bean定义跨越多个XML文件可能很有用。通常,每个单独的XML配置文件都代表体系结构中的逻辑层或模块

<!--第一种-->
<import resource="{path}/beans.xml"/>
<!--第二种-->
<beans>
    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>
    <import resource="/resources/themeSource.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>
</beans>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值