码迷,mamicode.com
首页 > 其他好文 > 详细

Bean

时间:2020-02-17 16:01:52      阅读:53      评论:0      收藏:0      [点我收藏+]

标签:ati   ext   oca   instance   pre   class   work   val   struct   

配置bean对象

<?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">
    <bean id="UserService" class="com.spring.service.impl.UserServiceImpl" scope="prototype">
        <!-- 构造方法注入 -->
        <constructor-arg name="name" value="tujiawei"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <!-- 
            除基本类型和String 
        -->
        <constructor-arg name="birtyday" ref="now"></constructor-arg>
    </bean>
    <bean id="now" class="java.util.Date"></bean>
    
    <!-- set 方法注入 -->
    <bean id="UserService" class="com.spring.service.impl.UserServiceImpl" scope="prototype">
        <property name="age" value="20"></property>
        <property name="name" value="tujiawei"></property>
    </bean>
    <bean id="now" class="java.util.Date"></bean>
</beans>

获取对象

ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IUserService us = ac.getBean("UserService", IUserService.class);

Bean

标签:ati   ext   oca   instance   pre   class   work   val   struct   

原文地址:https://www.cnblogs.com/tujw/p/12321838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!