常用注解-属性注入常用注解

 

 

 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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">

    <!--启动注解驱动,指定对应扫描路径,也就是资源所在的包-->
    <context:component-scan base-package="com.itheima"/>

   <!-- <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>-->
    <!--<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
    <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>-->


</beans>
package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/*定义bean,后面设置bean的id*/
@Component("userService")
/*设置bean的作用域*/
@Scope("singleton")
public class UserServiceImpl implements UserService {
    @Value("3")
    private int num;
    @Value("itheima")
    private String version;
    @Autowired
    private UserDao userDao;

    @Autowired
    private BookDao bookDao;

   /* public void setNum(int num) {
        this.num = num;
    }

    public void setVersion(String version) {
        this.version = version;
    }*/

    public void save(){
        System.out.println("user service running......"+num+"  "+version);
        userDao.save();
        bookDao.save();
    }
    /*设置bean的什么周期*/
    @PostConstruct
    public void  init(){
        System.out.println("user service init....");
    }
    /*设置bean的什么周期*/
    @PreDestroy
    public void destroy(){
        System.out.println("user service destroy.....");
    }
}
package com.itheima.service;

public interface UserService {
    public void save();
}
import com.alibaba.druid.pool.DruidDataSource;
import com.itheima.dao.BookDao;
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        UserService userService = (UserService) ctx.getBean("userService");
        userService.save();

       /* UserDao userDao = (UserDao) ctx.getBean("userDao");
        userDao.save();

        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
        bookDao.save();*/

       /* DruidDataSource dataSource = (DruidDataSource) ctx.getBean("dataSource");
        System.out.println(dataSource);*/
    }
}
public interface UserDao {
    public void save();
}
package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component("userDao")
public class UserDaoImpl implements UserDao{
    public void save(){
        System.out.println("user dao running.....");
    }
}
package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import org.springframework.stereotype.Component;

@Component("userDao1")
public class UserDaoImpl1 implements UserDao{
    public void save(){
        System.out.println("user dao 1 running.....");
    }
}
package com.itheima.dao;

public interface BookDao {
    public void save();
}
package com.itheima.dao.impl;

import com.itheima.dao.BookDao;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component
@Primary
public class BookDaoImpl implements BookDao {
    public void save(){
        System.out.println("book dao running;");
    }
}
package com.itheima.dao.impl;

import com.itheima.dao.BookDao;
import org.springframework.stereotype.Component;

@Component
public class BookDaoImpl1 implements BookDao {
    public void save(){
        System.out.println("book dao 1 running;");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值