1_Bean的注册方式

Spring中注册方式分为Xml注册和JavaConfig两种方式

基于xml方式

1、创建Bean对应的类

public class User {
    private String name;
    
    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public String toString() {
       return "User{" +
				"name='" + name + '\'' +
				'}';
    }
}

2、xml中配置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和Bean对应的类型-->
    <bean id="user" class="xxx.User">
        <!-- 使用字段名进行匹配,字段对应的值可放在value中,也可以在当前标签中增加 -->
    	<property name="name" value="张三"></property>
    </bean>
    
</beans>

3、基于xml方式的IOC初始化测试

public class BeanTest {
    @Test
    public void xmlIocTest() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("配置文件路径");
        // 通过xml配置中指定的bean id获取对应的对象
        User user = ac.getBean("user");
        System.out.println(user);
    }
}

基于Annotation方式注册Bean

在Spring中,它提供了Component,Service,Controller,Repository注解用于我们创建对象,四个注解基本功能一致

方式一:xml+Annotation
1)准备

a)引入aop的jar包
b)引入context名称空间,开启组件扫描
c)在类上使用注解,因为四个注解功能基本一致,前期使用的时候可以随意使用其中一个,添加注解后注入的名称默认为首字母小写的类名,当然也可以指定注入名称

开启组件扫描

<?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="包路径"/>

</beans>

使用注解注册

// 这里使用注解,相当于xml中的<bean id="heheheheh" class="类路径"/>
// 注解里面的value可以省略,默认为类名首字母小写--”cupImpl“
@Component(value = "heheheheh")
public class CupImpl {
	public void constitute() {
		System.out.println("make of glass");
	}
}

@Test
public void test(){
	ApplicationContext context = new ClassPathXmlApplicationContext("配置文件路径");
	CupImpl cup = context.getBean("heheheheh", CupImpl.class);
	cup.constitute();//make of glass
}

方式二: 全注解开发


配置扫描包路径的注意点
在xml中配置的扫描包默认是扫描配置包路径下的所有注解,如果想只扫描部分注解或者部分注解不想扫描,可使用下面的配置

<!-- 
`use-default-filters="false"`不再使用默认的filter,使用自定义配置,`context:include-filter`设置能够扫描那些内容,整个配置的意思为:
配置组件扫描,但是不使用默认配置,使用自定义配置,扫描带了Controller注解的类
 -->
<context:component-scan base-package="包路径" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!-- 配置路径扫描,使用默认扫描配置,除了被Controller注解修饰的类 -->
<context:component-scan base-package="包路径">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

xml方式和Annotation方式比较

1)灵活性:xml具有一定的局限性,使用配置类的方式可以更加清晰的实现定制化开发

2)安全性:JavaConfig属于类型安全,如果类名错误,会在编译期间提示。xml方式只有在启动或者运行阶段才会提示错误

3)部署性:JavaConfig类型修改后需要重新编译后才能生效,xml方式修改重启后生效

4)JavaConfig的可读性更高

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值