网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Spring基于注解的装配方式-创新互联

基于注解的装配方式

创新互联公司是专业的佳木斯网站建设公司,佳木斯接单;提供网站建设、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行佳木斯网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

在Spring中,使用XML配置文件可以实现Bean的装配工作,但在实际开发中如果Bean的数量过多,导致XML配置文件过于臃肿,给后期维护和升级带来一定的困难。为解决此问题,Spring提供了注解,通过注解也可以实现Bean的装配。

Spring常用注解:

注解描述
@Component指定一个普通的Bean,可以作用在任何层次
@Controller指定一个控制器组件Bean,用于将控制层的类标识为Spring中的Bean,功能上等同于@Component
@Service指定一个业务逻辑组件Bean,用于将业务逻辑层的类标识为Spring中的Bean,功能上等同于@Component
@Repository指定一个数据访问组件Bean,用于将数据访问层的类标识为Spring中的Bean,功能上等同于@Component
@Scope指定Bean实例的作用域
@Value指定Bean实例的注入值
@Autowired指定要自动装配的对象
@Resource指定要注入的对象
@Qualifier指定要自动装配的对象名称,通常与@Autowired联合使用
@PostConstruct指定Bean实例完成初始化后调用的方法
@PreDestroy指定Bean实例销毁前调用的方法

注解装配示例:

第一步: 在IDEA中创建新的Maven项目,然后在pom.xml文件中加载需要使用到的Spring四个基础包(spring-core-5.2.8.RELEASE.jar、spring-beans-5.2.8.RELEASE.jar、spring-context-5.2.8.RELEASE.jar、spring-expression-5.2.8.RELEASE.jar)以及Spring的一个依赖包(commons-logging-1.2.jar),除此之外,还要导入spring-aop-5.2.8.RELEASE.jar依赖包:

org.springframeworkspring-aop5.2.8.RELEASE复制代码

第二步: 创建applicationContext.xml,在该文件中引入Context约束并启动Bean的自动扫描功能(扫描出包下所有的类,进行注解解析)

复制代码

第三步: 定义实体类,新建entity包,在entity包下创建User实体类

@Component("student")
@Scope("singleton")
public class User{
    @Value("2020001234")
    private int stuId;
    @Value("何小幸")
    private Spring name;
    //省略getter、setter和toString()
}
复制代码

第四步: 定义dao层,创建StudentDao接口作为数据访问层接口,并在StudentDao接口中声明save()方法,用于查询Student实体的对象信息

package com.hexiaoxing.dao
public interface StudentDao{
    public void save();
}
复制代码

第五步: 创建StudenDaoImpl作为StudenDao的实现类,并在StudenDaoImpl类中实现StudentDao接口中的save()方法

@Repository("studentDao")
public class UserDaoImpl implements StudentDao{
    public void save(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student)applicationContext.getBean("student");
        System.out.println(student);
        System.out.println("执行UserDaoImpl.save()");
    }
}
复制代码

第六步: 定义service层,创建StudentService接口作为业务逻辑层接口,并在StudentService接口中定义save()方法

package com.hexiaoxing.service
public interface StudentService{
    public void save();
}
复制代码

第七步: 定义service层,创建StudentServiceImpl作为StudentService的实现类,并在StudentServiceImpl类中实现StudentService接口中的save()方法

@Service("studentService")
public class StudentServiceImpl implements StudentService{
    //使用@Resource注解注入StudentDao
    @Resource(name="studentDao")
    private StudentDao studentDao;
    public void save(){
        this.studentDao.save();
        System.out.println("执行StudentServiceImpl.save()");
    }
}
复制代码

第八步: 定义controller层,创建StudentController类作为控制层

@Controller
public class StudentController{
    //使用@Resource注解注入StudentDao
    @Resource(name="studentDao")
    private StudentService studentService;
    public void save(){
        this.studentService.save();
        System.out.println("执行StudentController.save()");
    }
}
复制代码

到目前,我们定义了student类,在dao层定义并实现了save()接口方法,方法中获取了student对象,在service中也定义了save来调用dao层的save,就这样,controller->service->dao三层,分别调用上一层的save方法

第九步: 定义测试类,在类中通过Spring容器加载配置文件并获取UserController实例,然后调用实例中的save()方法,查看结果

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站栏目:Spring基于注解的装配方式-创新互联
链接URL:http://bjjierui.cn/article/doohhi.html

其他资讯