前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java之spring之配置讲解

java之spring之配置讲解

作者头像
Vincent-yuan
发布2019-09-11 15:24:47
3390
发布2019-09-11 15:24:47
举报
文章被收录于专栏:Vincent-yuan

首先目录结构如下:

1. User.java

代码语言:javascript
复制
 1 package cn.sxt.vo;
 2 
 3 import java.util.Date;
 4 
 5 public class User {
 6     
 7     private String name;
 8     private int age;
 9     private Date birthday;
10     public String getName() {
11         return name;
12     }
13     public void setName(String name) {
14         this.name = name;
15     }
16     public int getAge() {
17         return age;
18     }
19     public void setAge(int age) {
20         this.age = age;
21     }
22     public Date getBirthday() {
23         return birthday;
24     }
25     public void setBirthday(Date birthday) {
26         this.birthday = birthday;
27     }
28     @Override
29     public String toString() {
30         return "User [name=" + name + ", age=" + age + ", birthday=" + birthday + "]";
31     }
32 }

2. beans.xml

代码语言:javascript
复制
<?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">
      <!-- 
          import用于导入其他配置信息  主要的作用就是团队协作开发使用
       -->
      <import resource="context.xml"/>
</beans>

3. context.xml

代码语言:javascript
复制
<?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">
    <description>
        描述信息
    </description>
    <!-- 
        bean 表示java对象  id是对象的标识符,在容器中唯一。通过标识符可以从容器中获取对象。
        name 如果没有配置id,那么name将作为对象的标识符,如果配置了id,那么是id的别名。
        可以同时设置多个别名,多个别名之间用分隔符(逗号,空格,分号)分割
        class 类所在的完全限定名=包名+类名;
     -->
    <bean id="user" name="u1,u2 u3;u4" class="cn.sxt.vo.User">
        <!-- property 表示类的属性设置,需要为其提供set方法,以便将值设置到对象上。
              name 表示set方法去掉set后的名称
              value 设置属性的值,value可以将基本数据类型和String设置到属性上。
              如果值是一个对象 需要使用ref属性引用
         -->
        <property name="name" value="张三疯"/>
        <property name="age" value="22"/>
    </bean>
    <!-- 设置别名  通常设置一个
        name表示要设置别名的对象的标识符
        alias 表示 设置的别名名称
    -->
    <alias name="user" alias="u5"/>
</beans>

4. SpringTest.java

代码语言:javascript
复制
package cn.sxt.spring;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.sxt.vo.User;

public class SpringTest {
    @Test
    public void testHello(){
        
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        User u=(User)ac.getBean("u4");
        System.out.println(u);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-07-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档