Spring注解终结

Spring注解的方式实现对象的管控
		注解作用与XML形式的作用是一样的

	
	用于对象创建的注解<bean id="" class="">
		@Component(value="id")
			value可以省略   @Component("id")
			小括号都可以省略@Component	id是默认  类名字的驼峰式
		@Controller
		@Service
		@Depository
	用于对象创建后的机制<bean id="" class="" scope="singleton" lazy-init="false" init-method destroy-method>
		@Scope("singleton | prototype多例")
		@Lazy(true)
		@PostConstructor	对象加载就执行的方法	写在方法上面
		@PreDestroy		对象被回收执行的方法	写在方法上面
	对象中可能含有属性
		属性的自动注入DI
		@Autowired	属性上面  属性对应的set方法上面  带有参数的构造方法上面
		@Qualifier	属性对象注入的微调整  属性有多个对象对应  选择其中一个
		@Value("值")	还支持SpEL	@Value("${值}")
		@Resource(name="")不是Spring家族提供的注解   Java提供的  统一性来讲不好


	Spring中提供新的注解
	用来解决那些类不是我们自己创建的
	但却想要通过注解的方式来管理

	1.需要自己写一个类ConfigClass
		类的目的是与之前配置文件的目的一致
		告知Spring加载初始化的时候该如何创建,管理对象

		类中可以自己设计方法
		方法通常是用来创建对象   与Spring底层加载<bean>标签 反射对象的作用一直
		方法上面添加@Bean
	2.需要在自定义类的上面进行一个描述
		添加@Configuration
		告知Spring这个类是我的配置 需要让他干活
	3.创建BeanFactory工厂
		BeanFactory factory = new AnnotationConfigApplicationContext(ConfigClass.class);
		factory.getBean("id");
	4.自己创建的类中会设计方法
		方法是用来创建bean对象  对象通常是别人写好的类型 JdbcTemplate
		方法上面添加@Bean
		如果bean注解中不写id	方法名字即使默认对象的id
	5.如果自己写的类中除了自定义方法
		还需要告知Spring需要扫描其他包
		在自己类上面添加一个注解@ComponentScan(value={} | basePackages={})
		@ComponentScan(basePackages={"controller","service","dao","domain"})
	6.如果在自己类中创建对象需要一些动态的参数
		可以使用外部配置文件的方式引入
		工程内创建一个文件    .properties
		当前主配置类中添加注解
		@PropertySource("classpath:jdbc.properties")
		可以在配置类中添加属性
		属性以@Value("${key}")
	7.如果主配置中方法过多
		且方法之间有些没有必然关联
		主配置类拆开
		变成多个小配置类
		主配置类中通过@Import(小配置类.class)进行引入

public class TestMain {

    public static void main(String[] args) {
//        BeanFactory factory = new ClassPathXmlApplicationContext("ApplicationContext.xml");
//        StudentController controller = (StudentController) factory.getBean("studentController");
//        controller.contollerMethod("男");


//        Student student1 = (Student)factory.getBean("student");
//        Student student2 = (Student)factory.getBean("student");
//        System.out.println(student1);
//        System.out.println(student2);
//        System.out.println(student1==student2);


//        StudentController controller = (StudentController)factory.getBean("studentController");
//        System.out.println(controller);
//        System.out.println(controller.getService());


//        Student student = (Student)factory.getBean("student");
//        System.out.println(student);
//        System.out.println(student.getTestInterface());
//        System.out.println(student.getSid()+"--"+student.getSname()+"--"+student.getSsex()+"--"+student.getSage());


        //通过扫描注解的方式获取一个工厂对象 改变创建工厂的类
        BeanFactory factory = new AnnotationConfigApplicationContext(MainConfig.class);
        StudentController controller = (StudentController) factory.getBean("studentController");
        controller.contollerMethod("男");

    }
}


/**
 * 控制层
 *      1.继承父类  2.重写方法  3.参数  返回值  异常。。。。
 *      封装-----没有继承  方法随便   参数随便(IOC DI)  返回值(void String ModelAndView)  没有异常
 *
 *      控制层方法做的事情
 *      1.接收请求参数信息
 *      2.调用业务层的小弟支持我
 *      3.根据业务层执行  最终控制响应信息
 */
@Controller("studentController")
public class StudentController {

    //属性  业务层小弟
    @Autowired
    private StudentService service;


    public StudentService getService() {
        return service;
    }

    //自己控制方法
    public void contollerMethod(String ssex){
        System.out.println("controllerMethod");
        service.serviceMethod(ssex);
    }
}


/**
 * 业务层
 *          为了controller做业务支持
 *
 *          业务层中的方法主要做   比较  计算  判断  等核心业务
 *          1.参数通常是controller给的
 *          2.核心业务是自己处理
 *                  需要数据的支持----DAO
 */
@Service("studentService")
public class StudentService {

    //属性 DAO小弟
    private StudentDao dao;
    @Autowired
    public void setDao(StudentDao dao) {
        this.dao = dao;
    }

    //自己业务方法
    public void serviceMethod(String ssex){
        System.out.println("serviceMethod");
        dao.select(ssex);
    }
}

/**
 * 数据持久层
 *
 *          操作数据库读写     JDBC+SQL
 */
@Repository("studentDao")
public class StudentDao {

    //属性 小弟JdbcTemplate     SqlSession
    @Autowired
    private JdbcTemplate jdbcTemplate;


    //dao自己的方法
    public void select(String ssex){
        System.out.println("daoMethod");
        String sql = "select * from student where ssex = ?";
        List list = jdbcTemplate.queryForList(sql,ssex);
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }
    }

}

  • jdbc.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/newtest?useSSL=false&characterEncoding=UTF-8
user=root
password=123456
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值