Spring框架总结——第二部分(基于xml配置+注解的方式配置IOC)

认知:不管是xml配置文件还是注解,实现的功能和作用是一样的,都是进行对IOC的配置,都是降低程序之间的耦合,只是配置方式不一样。

 

在实际开发xml配置文件和注解 怎么选择?

每家公司的使用习惯不一样,具体选择看公司使用那种方式。

 

基于注解的IOC配置

1、 搭建环境

导包

编写配置文件(用来开启扫描注解的,否则配置失败)

在类的跟路径下创建applicationContext.xml文件。导入约束,开启注解扫描

<beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:context="http://www.springframework.org/schema/context"

       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

                http://www.springframework.org/schema/context

                 http://www.springframework.org/schema/context/spring-context.xsd">

<!—开启扫描注解,依据注解创建对象,并存入容器中 -->

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

 

</beans>

 

使用注解来配置管理的类

 

用来创建对象的注解    相当于:<beanid="" class="">

@Component注解:

作用:把当前类交给Spring框架的IOC容器去创建对象,存储对象。

属性:value指定bean的id。如果不写,默认时当前类 的类名,首字母小写。

@Controller一般用于表现层的注解。

     @Service一般用于业务层的注解。

     @Repository一般用于持久层的注解。

细节:如果注解中有且只有一个属性要赋值时,且名称是valuevalue在赋值是可以不写。

 

用来注入属性的注解    相当于:<property name="" ref="">

@Autowired:

       作用:自动按照类型注入。只能注入其他bean类型

              当使用注解注入属性时,set方法可以省略。

 

@Value:

作用:注入基本数据类型和String类型数据的

属性:

    value:用于指定值

 

 

@Resource

作用:直接按照bean的id注入,只能注入bean类型。

属性name:指定bean的id。

 

用来改变作用范围的(对象的生命周期)       相当于<bean id =”” class=”” scope=”” />

@Scope:用来指定bean的作用范围的。

属性value:指定作用范围的值。

取值:singleton(默认单例)、prototype(多例)

 

 

和生命周期相关的初始化方法 销毁方法(了解)

@PostConstruct

作用:指定初始化方法

@PreDestroy:

作用:指定销毁方法。

 

直接上代码演示

 

持久层代码

@Repository(value="customerDao")

public class CustomerDaoImpl implements CustomerDao{

 

    public void save() {

       // TODO Auto-generated method stub

       System.out.println("--持久层保存了。。");

    }

   

    @PostConstruct

    public void init() {

       System.out.println("--初始化方法");

    }

    @PreDestroy

    public void   destory() {

       System.out.println("--销毁方法");

    }

}

 

业务层代码

@Service(value="customerService")

public class CustomerServiceImpl implements CustomerService {

 

    //注入bean属性

    @Resource(name="customerDao")

    private CustomerDao customerDao;

   

    //注入String和基本数据类型属性

    @Value(value="我是一条快乐的小语句..")

    private String Msg;

 

    public void save() {

       System.out.println("--业务层保存了.."+Msg);

       customerDao.save();

    }

}

测试代码

/*

 * 使用配置文件+注解的方式来配置IOC

 */

public class Demo {

    @Test

    public void fun1() {

       //获取容器

       ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

       //获取根据id对象

       CustomerService cs =(CustomerService) ac.getBean("customerService");

       cs.save();

    }

}

配置文件

<?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.itheima"/>

</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值