spring注解配置

spring注解简单配置

导入依赖

<!--spring依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

        <!--日志包-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

首先在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: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.lyf"></context:component-scan> 
</beans>

创建person类

public class Person {
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

创建Persondao,并添加@Repository

@Repository
public class PersonDao {


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

    }
}

创建personservice并添加@Service,相当于在xml中配置了一个bean标签
zaiservice使用@Autowired注入dao层对象

//相当于在xml中配置了一个bean标签
@Service
public class PersonService {
    private static  final Logger log= LoggerFactory.getLogger(PersonService.class);
    @Autowired
    PersonDao personDao ;

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

    }
}

测试

public class Test01SpringIoc {
    private static  final Logger log= LoggerFactory.getLogger(Test01SpringIoc.class);

    private ClassPathXmlApplicationContext context;
    @Before
    public void init(){
        //创建ioc 容器对象,暂时看成map
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
    @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+"");
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值