spring bean 不使用注入的方式获取的两种方式

部署运行你感兴趣的模型镜像

非注入方式取得spring注入bean的util类实现
第一种,我用在webservice接口中。
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * SpringBean的工具类<br>
 * 非注入方式取得spring注入bean的util类实现
 * @author langkai
 *
 */
public final class SpringBeanUtil implements ApplicationContextAware {

    private static ApplicationContext ctx;

    /**
     * 通过spring配置文件中配置的bean id取得bean对象
     * @param id spring bean ID值
     * @return spring bean对象
     */
    public static Object getBean(String id) {
        if (ctx == null) {
            throw new NullPointerException("ApplicationContext is null");
        }
        return ctx.getBean(id);
    }

  @Override
  public void setApplicationContext(ApplicationContext applicationcontext)
      throws BeansException {
    ctx = applicationcontext;
  }

}
实现ApplicationContextAware的Bean,在Bean被初始后,将会被注入ApplicationContext的实例。
applicationContext.xml
<bean class="*.SpringBeanUtil"/>
这样在spring配置文件加载时会自动执行ApplicationContextAware的setApplicationContext方法,将applicationContext对象传递给我们的Util类。
SpringBeanUtil.getBean("myBean");
在某些时候我们不希望通过注入也能取得某些bean时有用。
PS:
Spring 中提供一些Aware相关接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源,例如实现BeanFactoryAware的Bean在初始后,Spring容器将会注入BeanFactory的实例,而实现ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。  
 
  ================================
第二种,就是传统的Application程序使用的了。
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("springConfig_bus.xml");
    IMsgBusService client = (IMsgBusService) context.getBean("client");

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

SpringBean注入方式主要分为基于注解和基于 XML 两种类型,以下为你详细介绍: ### 使用注解注入 Bean - **使用注解(@Component、@Service、@Repository、@Controller)和 @Autowired**:`@Component` 是一个通用的注解,用于将类标记为 Spring 管理的 Bean。`@Service`、`@Repository` 和 `@Controller` 是 `@Component` 的特定形式,分别用于服务层、数据访问层和表现层。`@Autowired` 用于自动装配 Bean。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service class MyService { private MyRepository myRepository; @Autowired public MyService(MyRepository myRepository) { this.myRepository = myRepository; } } ``` - **使用 @Configuration 和 @Bean**:`@Configuration` 用于标记一个类为配置类,`@Bean` 用于在配置类中定义 Bean。 ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } } ``` - **使用构造函数注入**:通过构造函数将依赖的 Bean 注入到目标 Bean 中。 ```java class MyBean { private AnotherBean anotherBean; public MyBean(AnotherBean anotherBean) { this.anotherBean = anotherBean; } } ``` - **使用 @Value 注入属性值**:`@Value` 用于注入属性值,可以从配置文件或环境变量中获取。 ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component class MyComponent { @Value("${my.property}") private String myProperty; } ``` - **使用 @Primary 标注优先注入Bean**:当有多个候选 Bean 时,使用 `@Primary` 标注的 Bean 将被优先注入。 ```java import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; @Component @Primary class PrimaryBean implements MyInterface { // 实现接口方法 } ``` ### 基于 XML 注入 Bean - **构造器注入**:通过构造函数注入依赖的 Bean。 ```xml <bean id="myBean" class="com.example.MyBean"> <constructor-arg ref="anotherBean"/> </bean> ``` - **Setter 注入**:通过 setter 方法注入依赖的 Bean。 ```xml <bean id="myBean" class="com.example.MyBean"> <property name="anotherBean" ref="anotherBean"/> </bean> ``` - **接口注入(与 Spring 的 FactoryBean 结合使用)**:通过实现 `FactoryBean` 接口来创建 Bean。 ```java import org.springframework.beans.factory.FactoryBean; import org.springframework.stereotype.Component; @Component class MyFactoryBean implements FactoryBean<MyBean> { @Override public MyBean getObject() throws Exception { return new MyBean(); } @Override public Class<?> getObjectType() { return MyBean.class; } } ``` - **集合注入**:注入集合类型的依赖。 ```xml <bean id="myBean" class="com.example.MyBean"> <property name="myList"> <list> <ref bean="bean1"/> <ref bean="bean2"/> </list> </property> </bean> ``` - **自动装配(Autowiring)注入**:Spring 自动根据类型或名称进行 Bean 的装配。 ```xml <bean id="myBean" class="com.example.MyBean" autowire="byType"/> ``` - **引入(inner beans)**:在一个 Bean 定义中嵌套另一个 Bean 定义。 ```xml <bean id="myBean" class="com.example.MyBean"> <property name="innerBean"> <bean class="com.example.InnerBean"/> </property> </bean> ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值