Day21SSM之SpringDI

Spring依赖注入-Xml方式 Dao

-(1)对象注入

public class A{
   private int id;
   private B b;
}
public class XXXService{
   private int id;
   private XxxDao xxxdao;
}

Test

 @Test
    public void test09(){
       //PersonService personService = new PersonService();
       PersonService personService = (PersonService) context.getBean("personService");
       Person p  = new Person();
       p.setUsername("jack");
       p.setPassword("12345");
       boolean flag =personService.login(p);
       log.debug(flag+"");
    }

PersonService

public class PersonService {
    private static  final Logger log= LoggerFactory.getLogger(PersonService.class);
    //private PersonDao personDao = new PersonDao();
    private PersonDao personDao ;

    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }

    public boolean login(Person p) {
        log.debug(p+" login");
        Person person = personDao.find(p);
        if(person==null) {
            return false;
        }else{
            return true;
        }

    }
}

PersonDao

public class PersonDao {
    public Person find(Person p) {
        if("jack".equals(p.getUsername())&&"12345".equals(p.getPassword())){
            return p;
        }else{
            return null;
        }
    }
}

applicationContext.xml


    <bean id="personService" class="com.wzx.service.PersonService">
            <property name="personDao" ref="personDao"/>
    </bean>
    <bean id="personDao" class="com.wzx.dao.PersonDao">
    </bean>

Spring依赖注入-注解创建对象***

  • (1)对象比较多的话,开启注解扫描
<?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">


    <!--
        使用注解方式进行创建对象
        1.开启注解扫描

        含义:开启注解扫描,指定了 base-package 扫描指定的包,扫描包与子包中所有的类
        查看类上是否有指定的注解, 如果类上有指定的注解,那么就创建给类对象,
        放到spring容器中
    -->
    <context:component-scan base-package="com.wzx"/>
    </beans>
  • (2)只有标记有注解的类,才会被创建对象且添加到ioc容器中
  • (3)四个注解
//@Component  //其他层
//@Repository //Dao层
//@Service    //Service层
@Controller("xxx")//Controller层
public class MyClass{
}
  • 注解没有配置id,但是默认是 myClass
    @Test
    public void test10(){

        PersonService personService = (PersonService) context.getBean("personService");
        log.debug(personService+" test10");

        PersonDao personDao = (PersonDao) context.getBean("personDao");//id为类名首字符小写
        log.debug(personDao +" test10");
    }

Spring依赖注入-注解实现注入***

  • (1)注入是什么?
    就是查找之后,进行赋值
  • (2)三种注入方式
    》1 ***
    @Autowired
    或者
    @Autowired
    @Qualifier(“bean的id”)
    》2
    @Value("#{bean的id}")
    》3
    @Resource(name=“bean的id值”)
@Service
public class PersonService {
   //private PersonDao personDao = new PersonDao();
    //第一种:@Autowired或者  @Autowired和@Qualifier("bean的id")搭配
    //第二种:@Value("#{bean的id}")
    //第三种:@Resource(name="bean的id值")
    @Autowired
    PersonDao personDao ;
 }
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翁老师的教学团队

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值