Bean

Posted tujw

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bean相关的知识,希望对你有一定的参考价值。

配置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的主要内容,如果未能解决你的问题,请参考以下文章

报错“Field pet in XXX.HelloController required a bean of type ‘XXX.Pet‘ that could not be found.“(代码片段

Bean后置处理器 - BeanPostProcessor#postProcessAfterInitialization

What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段

死磕 Spring----- IOC 之从单例缓存中获取单例 bean

[死磕 Spring 17/43] --- IOC 之从单例缓存中获取单例 bean

共享处理程序和支持 bean