Spring2.5注解开发--基础(简化开发)-过度

注解开发简化

注解开发定义bean
1.在配置文件中加第三方:让spring感知到组件component
<context:component-scan base-package="com.itheima"/>
// 用的最多的加载第三方配置文件:
<context:property-placeholder location="classpath:*.properties"/>

2.在dao实现类 和service实现类 定义bean
@Component("bookdao")  和 @Service 
  1. 注解开发定义bean对象
1.在applicationContext.xml中开启Spring注解包扫描
注: <context:component-scan base-package="com.itheima"/>

component-scan-->组件扫描 扫描所有的实现类的注解

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!--扫描com.itheima包及其子包下的类中注解-->
    <context:component-scan base-package="com.itheima"/>
</beans>
2.代码实现
1.接口  创建方法
2.实现类  加注解@component 创建空参  创建方法
3.测试类 
4.配置类
具体步骤

// 1.dao接口
public interface BookDao {
    public void save();
}
// 2.dao实现类
@Component("bookdao")
public class BookDaoImpl implements BookDao {
    @Override
    public void save() {
        System.out.println("bookdao 的save方法");
    }
}

// 3.servcie 接口
public interface BookService {
}
// 4.service 接口实现类
@Service
public class BookServiceImpl  implements BookService {
    //私有接口对象
    private BookDao bookDao;
   //set方法
    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }
  //创建service 方法 并用dao调用
    public void save(){
        System.out.println("这是在service中的方法");
        bookDao.save();
    }
}
// 5.测试类
public class App {
    public static void main(String[] args) {
        //1.加载配置文件
        ClassPathXmlApplicationContext ctt = new ClassPathXmlApplicationContext("applicationContext.xml");

        //2.获取bookdao的bean  //前面注解配置的bean名称   名称和类名共同获得
        BookDao bean = ctt.getBean("bookdao",BookDao.class);
        System.out.println(bean);

        //3.获得service中的bean   按照类型获得
        BookService bean1 = ctt.getBean(BookService.class);
        System.out.println(bean1);
    }

}

打印输出地址值:
com.itheima.dao.impl.BookDaoImpl@7b2bbc3
com.itheima.Service.impl.BookServiceImpl@235834f2
  1. spring注解开发依赖注入
1.自动装配
@Autowired  类型自动装配
@valuer    获得具体值

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><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">
<!--扫描com.itheima包及其子包下的类中注解-->
<context:component-scan base-package="com.itheima"/></beans>
2.代码实现
// 1.dao接口
public interface BookDao {
    public void save();
}
// 2.dao实现类
@Component("bookdao")
public class BookDaoImpl implements BookDao {
    @Override
    public void save() {
        System.out.println("bookdao 的save方法");
    }
}

// 3.servcie 接口
public interface BookService {
}
// 4.service 接口实现类
@Service
public class BookServiceImpl  implements BookService {
    @Autowired
    //私有接口对象
    private BookDao bookDao;
    @Value("tom")                //@value 获得name的值
    private String name;

   //set方法  为成员变量赋值
    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }
  //创建service 方法 并用dao调用
    public void save(){
        System.out.println("这是在service中的方法");
        bookDao.save();
    }

//tostring  返回具体的数值   不加打印只会显示地址值
    @Override
    public String toString() {
        return "BookServiceImpl{" +
                "bookDao=" + bookDao +
                ", name='" + name + '\'' +
                '}';
    }
}
//   app测试
public class App {
    public static void main(String[] args) {
        //1.加载配置文件
        ClassPathXmlApplicationContext ctt = new ClassPathXmlApplicationContext("applicationContext.xml");

        //2.获取bookdao的bean  //前面注解配置的bean名称   名称和类名共同获得
        BookDao bean = ctt.getBean("bookdao",BookDao.class);
        System.out.println(bean);

        //3.获得service中的bean   按照类型获得
        BookService bean1 = ctt.getBean(BookService.class);
        System.out.println(bean1);
    }

}

打印数值:
bookdao的空参构造
com.itheima.dao.impl.BookDaoImpl@69997e9d
BookServiceImpl{bookDao=com.itheima.dao.impl.BookDaoImpl@69997e9d, name='tom'}

注解开发和配置文件开发对比的不同点

// 1.定义bean区别
xml配置:使用的是bean标签  ioc-->id属性--class属性  
注解开发:使用注解 @Component(定义bean名称)-->分支注解@Service/@Controller/@Repository
// 2.依赖注入区别
xml配置:setter注入(set方法) /构造器注入(构造方法)/自动装配
注解开发:@Autowired (接口) @Value(值)
// 3.配置第三方bean
xml配置:bean标签 静态工厂/实例工厂/Factorybean
注解开发:@bean

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值