第一个 Spring 程序
3. HelloSpring
3.1 Hello
-
防止每次 reimport 都回到1.5,在pom.xml中加上java版本号
<build> <finalName>income</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build>
-
官网beans格式
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--bean可配置多个--> <bean id="..." class="..."> <!-- collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions go here --> </beans>
-
Hello程序
Hello类
package com.peng.pojo; public class Hello { private String str; public String getStr() { return str; } public void setStr(String str) { this.str = str; } @Override public String toString() { return "Hello{" + "str='" + str + "\'" + "}"; } }
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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!--使用Spring来创建对象,在Spring这些都被称为bean 类型 变量名 = new 类名(); Hello hello = new Hello(); id = 变量名 class = new的对象 property 给对象中的属性设置一个值 --> <bean id="hello" class="com.peng.pojo.Hello"> <property name="str" value="Spring"></property> </bean> </beans>
MyTest.java 测试类
import com.peng.pojo.Hello; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { //获取Spring的上下文对象 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //我们的对象现在都在Spring中管理,我们要使用,直接去里面取出来就可以! Hello hello = (Hello) context.getBean("hello"); System.out.println(hello.toString()); } }
3.2 上一节代码修改
-
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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="mysqlImpl" class="com.peng.dao.UserDaoMysqlImpl"></bean> <bean id="oracleImpl" class="com.peng.dao.UserDaoOracleImpl"></bean> <bean id="UserServiceImpl" class="com.peng.service.UserServiceImpl"> <!-- ref:引用Spring容器中创建好的对象 value:具体的值,基本数据类型 --> <property name="userDao" ref="mysqlImpl"/> </bean> </beans>
-
测试类
用容器操作,从此不用再new!
import com.peng.service.UserServiceImpl; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { public static void main(String[] args) { //获取ApplicationContext,拿到Spring的容器,官网固定用法 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); //需要什么get什么 UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl"); userServiceImpl.getUser(); } }
3.3 总结
IOC的理解:原来的对象都是由程序员new出来的,现在我们提供了spring容器,它可以创建出一个对象,并配置这个对象的属性。spring容器的配置文件xml是方便修改的,原来的程序只需被动接受spring提供的对象。一句话说:对象由Spring来创建,管理,装配!