Spring数据怎么在Java项目中进行单元配置

发布时间:2020-12-11 13:54:26 作者:Leah
来源:亿速云 阅读:152

本篇文章为大家展示了Spring数据怎么在Java项目中进行单元配置,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

1. 配置元数据

基于XML的配置元数据的基本结构: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-2.5.xsd">

 <bean id="..." class="...">
  <!-- collaborators and configuration for this bean go here -->
 </bean>

 <bean id="..." class="...">
  <!-- collaborators and configuration for this bean go here -->
 </bean>

 <!-- 更多的bean的时候 在引用的xml文件一定是要带spring dtd头的文件-->
  <import resource="services.xml"/>
</beans>

services.xml

在配置文件里面命名其实id 和name都是一样的

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 <bean id="userService" name="userService" class="com.sun.service.UserService">
<property name="name">
 <value>sunxin</value>
</property>
</bean>
</beans>

2. 实例化容器

ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"beans.xml"});

3. bean的别名

<!--name指向的是已经存在该id的bean,alias是给给该bean命的别名-->
<alias name="userService" alias="user"/>

调用可以通过:

UserService us = (UserService) app.getBean("user");

上述内容就是Spring数据怎么在Java项目中进行单元配置,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 在Spring中整合JUnit单元测试
  2. 怎么在Spring中对JavaConfig进行配置

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java spring ava

上一篇:利用idea怎么将java项目中打包成一个可执行的jar包

下一篇:怎么在Java项目中利用TCP与Socket 实现一个文件上传功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》