ssh2


创建一个jdbc.properties文件
url=jdbc:mysql://xx
user=root
在spring-config中创建applicationContext.xml文件
编写
<cxt:property-placeholder location="classpath:jdbc.properties"/>
<bean class="com.yededu.testmaven.m1.A" scope="prototype">
<property name="user" value="${user}" />
<property name="url" value="${url}" />
</bean>

可以在一个文件中写,如xx.xml
<bean class="com.yededu.testmaven.m1.A" scope="prototype">
<property name="user" value="${user}" />
<property name="url" value="${url}" />
</bean>

然后在另一个文件中用下列代替
<import resoure="classpath:xx.xml"/>

也可以
<cxt:property-placeholder location=“classpath:jdbc.properties"/>
<cxt:annotation-config />
<cxt:component-scan base-package="目录名"/>
目录下的类:
要加@Component当然不用加get和set了
默认值要用@Value("${url}")
使用另一个类要用@Autowired自动注入也可以用@Resource(这个使用较少)

@Component基础组件:@Service业务层、@Repository存储层、@Resource(name)

测试依赖
<dependencies>
<dependency>
<groupId>org.springframenwork</groupId>
<artifactId>spring-test<artifactId>
<version>5.1.9RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit<artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

测试类要加
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")

类用自动注入

测试方法加@Test标注

原文地址:https://www.cnblogs.com/xiao-c-s/p/12420037.html