Spring进阶案例

pom.xml(参考入门案例)
spring.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">

       <!--表示告知spring 要扫描的包
       这些包以及子类包当中的类上如果添加了@Component注解,这些添加了注解的类就交给spring容器创建对象-->
       <context:component-scan base-package="com.syh.pojo"></context:component-scan>
       <context:component-scan base-package="com.syh.dao"></context:component-scan>
       <context:component-scan base-package="com.syh.service"></context:component-scan>
       <context:component-scan base-package="com.syh.controller"></context:component-scan>
       <!--多包扫描的另一种方式-->
       <!--<context:component-scan base-package="
       com.syh.pojo,com.syh.dao,com.syh.service,com.syh.controller"></context:component-scan>-->
</beans>



实体类
package com.syh.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

//@Component注解标识在类上,表示对象由spring容器创建,value属性表示创建的id值(可省略),默认为类名(首字母小写)
//@Value注解是在对象创建完后才开始赋值,因此在刚创建对象时value值默认为null
@Component
public class Team {
    @Value("24")
    private Integer id;
    @Value("湖人队")
    private String name;
    @Value("洛杉矶")
    private String location;

    public Team() {
        System.out.println("team - 默认的构造方法:id="+id+",name="+name+",location="+location);
    }

    public Team(Integer id, String name, String location) {
        this.id = id;
        this.name = name;
        this.location = location;
        System.out.println("team - 带参的构造方法:id="+id+",name="+name+",location="+location);
    }

    @Override
    public String toString() {
        return "Team{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", location='" + location + '\'' +
                '}';
    }
}



package com.syh.dao;

import org.springframework.stereotype.Repository;

@Repository(value = "teamDao2")
public class TeamDao {

    public TeamDao() {
        System.out.println("teamDao - 默认的构造方法");
    }

    public void add(){
        System.out.println("teamDao - add");
    }
}



package com.syh.service;

import com.syh.dao.TeamDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class TeamService {

    @Autowired //自动装配,默认按照类型装配(required = true类型不存在时报错/false报空指针异常)
    @Qualifier("teamDao2") //按名称自动装配,需要两者搭配使用
    private TeamDao teamDao;//=new TeamDao();

    public TeamService() {
        System.out.println("teamService - 默认的构造方法");
    }

    public void add(){
        teamDao.add();
        System.out.println("TeamService - add");
    }
}



package com.syh.controller;

import com.syh.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class TeamController {

    //@Resource JDK版本高于1.6 默认按名称自动装配,如果没有名称相符,则按类型自动装配
    @Autowired
    private TeamService teamService;

    public void add(){
        teamService.add();
        System.out.println("teamController - add");
    }

    public TeamController() {
        System.out.println("teamController - 默认的构造方法");
    }
}



测试类
package com.syh.pojo;

import com.syh.controller.TeamController;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test01 {

    @Test
    public void Test01() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        Team team = (Team)ac.getBean("team");
        System.out.println(team);
        TeamController tc = (TeamController) ac.getBean("teamController");
        tc.add();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值