Spring的三种主要装配方式

1、隐式的bean发现机制和自动装配

@Component标记该类为组件,@Autowired注入 这两个注解是Spring中的注解,如果不想使用Spring的注解,可以分别用@Named和@Inject代替上述两个注解

(1)在实现类上面加上@Component标签,表明该类为组件类,并告知Spring要为这个类创建bean

public interface SysKeywordBlockService {
	List<String> getSysKeywordBlock(Long spId);
}
@Component
public class SysKeywordBlockServiceImpl implements SysKeywordBlockService {

    @Autowired
    private SysKeywordBlockMapper sysKeywordBlockMapper;

    public List<String> getSysKeywordBlock(Long spId) {
        Page page = new Page(1, 20);
        List<SysKeywordBlockEntity> sysKeywordBlockEntityList = sysKeywordBlockMapper.selectAll(page);
        List<String> keywords = new ArrayList<String>();
        for (SysKeywordBlockEntity sysKeywordBlockEntity : sysKeywordBlockEntityList) {
            keywords.add(sysKeywordBlockEntity.getKeyword());
        }
        return keywords;
    }
}

(2)启用@ComponentScan进行扫描或者定义xml进行扫描

@Configuration
@ComponentScan(basePackages = "com.mybatis.*")
public class SysKeywordConfig {

}

通过@ComponentScan来告诉spring扫描basePackages指定包下面的带有@Component的类,为该类创建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"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="com.mybatis.*"/>

</beans>

如果想要将第三方库中的组件装配到自己的应用中就不能使用自动装配了,因为没有办法在第三方类上添加@Component和@Autowired注解,所以就要使用显式装配的方式了

2、在java中进行显示配置

(1)创建配置类

@Configuration
public class SysKeywordConfig {

}

创建配置类的关键在于为其添加@Configuration注解,该注解表明这个类是一个配置类,该类应该包含在Spring应用上下文中如何创建bean的细节。

(2)声明简单的bean

public interface SysKeywordBlockService {
	List<String> getSysKeywordBlock(Long spId);
}
public class SysKeywordBlockServiceImpl implements SysKeywordBlockService {

    public void getSysKeywordBlock() {
      System.out.println("关键字");
    }
}

 

@Configuration
public class SysKeywordConfig {
    @Bean
    public SysKeywordBlockService sysKeywordBlockService(){
        return new SysKeywordBlockServiceImpl();
    }
}

在方法上添加@Bean注解,该注解会告诉Spring这个方法将会返回一个对象,该对象要注册为spring应用上下文中的bean,默认情况下bean的ID与带有@Bean注解的方法名一样,如果想为其设置成一个不同的名字,可以通过那么属性指定一个不同的名字:@Bean(name=“名字”);

(3)测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SysKeywordConfig.class)
public class SysKeywordBlockServiceConfigTest {

    @Autowired
    private SysKeywordBlockService sysKeywordBlockService;

    @org.junit.Test
    public void testQuerySysKeywordBlockList(){
        Assert.assertNotNull(sysKeywordBlockService);
    }
}

@ContextConfiguration标注我们想要导入这个测试类的某些bean,在此处表明注入SysKeywordConfig配置类中声名的bean

3、在xml中进行显示配置

(1)配置applicationContext.xml文件

<?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="sysKeywordBlockService" class="com.mybatis.service.impl.SysKeywordBlockServiceImpl"/>

</beans>

id表明该bean的名称,class指定的是实现类

public interface SysKeywordBlockService {
	List<String> getSysKeywordBlock(Long spId);
}
public class SysKeywordBlockServiceImpl implements SysKeywordBlockService {

    public void getSysKeywordBlock() {
      System.out.println("关键字");
    }
}

 (2)测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SysKeywordBlockServiceTest {

    @Autowired
    private SysKeywordBlockService sysKeywordBlockService;

    @org.junit.Test
    public void testQuerySysKeywordBlockList(){
         Assert.assertNotNull(sysKeywordBlockService);
    }

使用locations指定加载的配置文件名称

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值