Spring框架学习,day02

 示例:demo7

package com.jt.demo7_anno;

public interface Pet {
    void hello();
}
package com.jt.demo7_anno;

import org.springframework.stereotype.Component;

@Component
public class Pig implements Pet{
    @Override
    public void hello() {
        System.out.println("扮猪吃老虎!");
    }
}
package com.jt.demo7_anno;

import org.springframework.stereotype.Component;

@Component
public class Tiger implements Pet{
    @Override
    public void hello() {
        System.out.println("八分饱冲击!!!");
    }
}
package com.jt.demo7_anno;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class User {

    /*实现类有多个的前提下,会有Error报错
    * 报错信息为:expected single matching bean but found 2: pig,tiger
    * 一个注入信息中有多个实现类*/
    @Autowired
    @Qualifier(value = "pig")
    //@Qualifier必须和@Autowired联用,并且需要执行value的名称,就是spring中的key
    //准则:一般条件下,Spring都是单实现!!!
    private Pet pet;
    public void hello(){
        pet.hello();
    }
}

package com.jt.demo7_anno;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.jt.demo7_anno")
public class SpringConfig {

}
package com.jt.demo7_anno;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestUser {
    public static void main(String[] args) {
        ApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        User user = context.getBean(User.class);
        user.hello();
    }
}

示例:demo8

package com.jt.demo8;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
//当spring容器启动时,加载指定的配置文件,将数据保存到容器中
@PropertySource(value = "classpath:/dept.properties",encoding = "UTF-8")
public class Dept {
    //取值方式:${pro中的key}
    @Value("${dept.id}")
    private Integer id;
    @Value("${dept.name}")
    private String name;

//    @Value("101") //@Value注解可以为简单属性赋值
//    private Integer id;
//    @Value("财务部")
//    private String name;
    public void hello(){
        System.out.println("获取数据:"+id+":"+name);
    }
}
package com.jt.demo8;
//本类为demo8包的配置类
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.jt.demo8")
public class SpringConfig {

}
package com.jt.demo8;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestDept {
    public static void main(String[] args) {
        ApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        Dept dept = context.getBean(Dept.class);
        dept.hello();
    }
}

示例:demo9

package com.jt.demo9.pojo;

import java.io.Serializable;
/*1.属性都是私有的,方法是公共的
* 2.要求对象序列化    作用:1.保证数据按照正确的格式输出
*                       2.在多线程条件下,共享数据必须序列化
* 3.POJO的对象一般都是用来实现数据的传递的*/
public class User implements Serializable {
    private Integer id;
    private String name;
    private Integer age;
    private String sex;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
}
package com.jt.demo9.mapper;

import com.jt.demo9.pojo.User;

public interface UserMapper {
    void addUser(User user);
}
package com.jt.demo9.mapper;

import com.jt.demo9.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@Component //Spring管理 key:userMapperIml value:反射机制
@Repository
public class UserMapperImpl implements UserMapper{
    @Override
    public void addUser(User user) {
        System.out.println("完成用户的入库操作:"+user.getName());
    }
}
package com.jt.demo9.service;

import com.jt.demo9.pojo.User;

public interface UserService {
    void addUser(User user);
}
package com.jt.demo9.service;

import com.jt.demo9.mapper.UserMapper;
import com.jt.demo9.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceIml implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void addUser(User user) {
        userMapper.addUser(user);
    }
}
package com.jt.demo9.controller;

import com.jt.demo9.pojo.User;
import com.jt.demo9.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    public void addUser(){
        User user = new User();
        user.setId(101);
        user.setName("春节快乐");
        user.setAge(30);
        user.setSex("傍地走");
        userService.addUser(user);
    }
}
package com.jt.demo9.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.jt.demo9")
public class SpringConfig {
}
package com.jt.demo9;

import com.jt.demo9.config.SpringConfig;
import com.jt.demo9.controller.UserController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestMVC {
    public static void main(String[] args) {
        ApplicationContext context =
                new AnnotationConfigApplicationContext(SpringConfig.class);
        UserController user = context.getBean(UserController.class);
        user.addUser();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值