首页 技术 正文
技术 2022年11月15日
0 收藏 516 点赞 4,749 浏览 3097 个字

Java程序员常用的@Component、@Repository、@Controller、@Service系列【案例demo3】

Java程序员常用的@Component、@Repository、@Controller、@Service系列【案例demo3】

 

很多程序员通过在类上使用@Repository、@Component、@Service 和 @Constroller 注解,Spring会自动创建相应的 BeanDefinition 对象,并注册到 ApplicationContext 中。这些类就成了 Spring受管组件。这三个注解除了作用于不同软件层次的类,其使用方式与@Repository 是完全相同的。

处理类:org.springframework.context.annotation.ScannedGenericBeanDefinition

[if !supportLists]· [endif]项目包结构

F:.

├─java

│  └─com

│      └─example

│          └─demo3

│              │  Demo3Application.java

│              │

│              ├─controll

│              │      StuController.java

│              │

│              ├─dao

│              │      StuDao.java

│              │      StuDaoImp.java

│              │

│              ├─entity

│              │      Stu.java

│              │

│              └─server

│                      StuService.java

│                      StuServiceImp.java

└─resources

application.properties

project.text

[if !supportLists]· [endif]控制器角色StuController

package com.example.demo3.controll;

import org.springframework.stereotype.Controller;

@Controller

public class StuController {

}

[if !supportLists]· [endif]数据角色StuDao、StuDaoImp

package com.example.demo3.dao;

public interface StuDao {

}

package com.example.demo3.dao;

import org.springframework.stereotype.Repository;

@Repository

public class StuDaoImp implements StuDao{

}

[if !supportLists]· [endif]服务角色StuService、StuServiceImp

package com.example.demo3.server;

public interface StuService {

}

package com.example.demo3.server;

import org.springframework.stereotype.Service;

@Service

public class StuServiceImp implements StuService {

}

[if !supportLists]· [endif]其它组件角色Stu

package com.example.demo3.entity;

import org.springframework.stereotype.Component;

@Component

public class Stu {

String name;

public Stu(String name) {

this.name = name;

}

public Stu() {

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public String toString() {

return “Stu{” +

“name='” + name + ‘\” +

‘}’;

}

}

[if !supportLists]· [endif]Demo3Application(启动程序)

package com.example.demo3;

import com.example.demo3.controll.StuController;

import com.example.demo3.dao.StuDao;

import com.example.demo3.entity.Stu;

import com.example.demo3.server.StuService;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication

public class Demo3Application {

public static void main(String[] args) {

ConfigurableApplicationContext context = SpringApplication.run(Demo3Application.class, args);

//@Component注册的组件,名称默认都是类名的首字母小写

//纯属于注解方式注册组件

//之所以能力扫描到这些包,因为注解@AutoConfigurationPackage的作用(但必须满足所有组件都在启动类所在包的平级或子集)

StuController stuController = context.getBean(“stuController”, StuController.class);

Stu stu = context.getBean(“stu”, Stu.class);

StuDao stuDaoImp = context.getBean(“stuDaoImp”, StuDao.class);

StuService stuServiceImp = context.getBean(“stuServiceImp”, StuService.class);

//打印都有地址

System.out.println(stuController);

System.out.println(stu);

System.out.println(stuDaoImp);

System.out.println(stuServiceImp);

context.close();

}

}

AnnotationConfigApplicationContext与ConfigurableApplicationContext的关系

总结:

1.上下文基本架构关系

2.ConfigurableApplicationContext:

3.ClassPathXmlApplicationContext:

4.AnnotationConfigApplicationContext:

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295