标签:rop XML video his sys 控制 方法 port mave
可以参考:https://www.cnblogs.com/nwu-edu/p/9542074.html,为了简化直接导入webmvc的依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
package com.dreamcold.demo.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 + ‘\‘‘ +
‘}‘;
}
}
类型 变量名=new 类型();
id 等价于变量名
class 全路径类名
property 给对象的属性设置一个值
<?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-3.0.xsd">
<bean id="hello" class="com.dreamcold.demo.pojo.Hello">
<property name="str" value="spring"></property>
</bean>
</beans>
package com.dreamcold.demo;
import com.dreamcold.demo.pojo.Hello;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloTest {
@Test
public void test(){
//获取Spring的上下文对象
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
//我们的对象都在Spring中进行管理了,我们一旦使用直接从中取出来就可以了
Hello hello= (Hello) context.getBean("hello");
System.out.println(hello.toString());
}
}
在上一个案例中,我们可以让Spring来管理对象,我们首先要创建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-3.0.xsd">
<bean id="MysqlImpl" class="com.dreamcold.dao.UserDaoMysqlImpl">
</bean>
<bean id="OracleImpl" class="com.dreamcold.dao.UserDaoOracleImpl">
</bean>
<bean id="ServiceImpl" class="com.dreamcold.service.UserServiceImpl">
<property name="userDao" ref="MysqlImpl"></property>
<!-- 普通对象用value,引用对象用ref 引用Spring中创建好的对象 -->
<!-- Value是一个具体的值,比如基本数据类型 -->
</bean>
</beans>
测试一下
import com.dreamcold.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
UserService userService=(UserService)context.getBean("ServiceImpl");
userService.getUser();
}
}
我们可以思考这样的好处,假如我们底层要修改实现的话,比如修改为Oracle实现,那么仅仅需要修改配置文件就可以了
<?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-3.0.xsd">
<bean id="MysqlImpl" class="com.dreamcold.dao.UserDaoMysqlImpl">
</bean>
<bean id="OracleImpl" class="com.dreamcold.dao.UserDaoOracleImpl">
</bean>
<bean id="ServiceImpl" class="com.dreamcold.service.UserServiceImpl">
<property name="userDao" ref="OracleImpl"></property>
<!-- 普通对象用value,引用对象用ref 引用Spring中创建好的对象 -->
<!-- Value是一个具体的值,比如基本数据类型 -->
</bean>
</beans>
这样我们就避免了修改代码
学习自链接:狂神说
Spring入门系列-创建一个Spring的helloworld项目
标签:rop XML video his sys 控制 方法 port mave
原文地址:https://www.cnblogs.com/mengxiaoleng/p/14940343.html