Spring 获取注解形式下注入的Bean

本文介绍了在Spring框架中,如何通过@Component等注解创建Bean,并详细解析了Spring自动生成Bean名称的规则。当注解未指定Bean名称时,Spring会根据类名生成默认名称,通常为首字母小写的类名,但若类名前两个字母均为大写,则保留原类名作为Bean名称。

使用spring注解形式注入bean,通过@Component、@Repository、 @Service和@Controller注解类,文档中说“注解如果没有指定bean的名字,默认为小写开头的类名”。例如类名是MyClass,则spring返回myClass的bean名。

但是如果类名前两个字母都是大写,则返回的bean名也是大写,即类名是MYClass,bean名也是MYClass。
举例:

@Component
public class MYClass{
}

获取MYClass spring bean: 
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
MYClass object = (MYClass)ctx.getBean("MYClass");



附Spring源码:

/**
     * Utility method to take a string and convert it to normal Java variable
     * name capitalization.  This normally means converting the first
     * character from upper case to lower case, but in the (unusual) special
     * case when there is more than one character and both the first and
     * second characters are upper case, we leave it alone.
     * <p>
     * Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
     * as "URL".
     *
     * @param  name The string to be decapitalized.
     * @return  The decapitalized version of the string.
     */
    public static String decapitalize(String name) {
        if (name == null || name.length() == 0) {
            //如果@Service定义了名字,直接返回定义的名字; e.g. @service("abc"),直接返回abc
            return name;
        }
    // 如果发现类的前两个字符都是大写,则直接返回类名
        if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
                        Character.isUpperCase(name.charAt(0))){
            return name;
        }
    // 将类名的第一个字母转成小写,然后返回
        char chars[] = name.toCharArray();
        chars[0] = Character.toLowerCase(chars[0]);
        return new String(chars);
    }

 

转载于:https://my.oschina.net/trydaydayup/blog/855467

Spring获取某个注入bean的指定方法,可先获取bean的实例,再调用其指定方法。 ### 方法一:通过`ContextLoader`获取`WebApplicationContext` 若在Web应用里,可借助`ContextLoader`获取`WebApplicationContext`,再通过它得到bean实例,进而调用指定方法。示例代码如下: ```java import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext; // 获取WebApplicationContext WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); // 获取指定ID的bean实例 Object bean = wac.getBean(beanID); // 假设bean是ValueGenerator类型,调用其getAgeValue方法 if (bean instanceof com.b510.app.util.ValueGenerator) { com.b510.app.util.ValueGenerator valueGenerator = (com.b510.app.util.ValueGenerator) bean; int age = valueGenerator.getAgeValue(); } ``` 此方法适用于Web应用环境,能在代码里获取Spring上下文并获取指定bean实例,调用其方法 [^3]。 ### 方法二:注入Bean并调用方法 可在类中直接注入需要的bean,之后调用其方法。示例代码如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MyService { @Autowired private com.b510.app.util.ValueGenerator valueGenerator; public void doSomething() { // 调用注入bean的指定方法 int age = valueGenerator.getAgeValue(); } } ``` 这种方法需在Spring管理的组件里使用`@Autowired`注解注入bean,随后调用其指定方法。 ### 方法三:使用`MethodInvokingFactoryBean`注入方法返回值 若想注入某个bean的方法返回值,可使用`MethodInvokingFactoryBean`。示例如下: ```xml <bean id="valueGenerator" class="com.b510.app.util.ValueGenerator" /> <bean id="ageValue" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="valueGenerator" /> <property name="targetMethod" value="getAgeValue" /> </bean> ``` 上述配置里,通过`MethodInvokingFactoryBean`注入`valueGenerator` bean的`getAgeValue`方法的返回值 [^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值