Spring 依赖注入 简单实例Demo

package com.knight.service;

public interface Knight {
	public void doAction();
}

 

package com.knight.service.impl;

import com.knight.service.Knight;
import com.quest.service.Quest;

public class BraveKnight implements Knight {

	private Quest quest;
	public  BraveKnight(Quest quest) {
		this.quest = quest;
	}
	@Override
	public void doAction() {
		quest.qhelp();

	}

}

 

package com.quest.service;

public interface Quest {
	public void qhelp();
}

 

package com.quest.service.impl;

import com.quest.service.Quest;

public class BitDragon implements Quest {

	public BitDragon() {
	}

	public void qhelp() {
		System.out.println("bit dragon");
	}

}

 

package com.quest.service.impl;

import com.quest.service.Quest;

public class RescueBeauty implements Quest {

	public RescueBeauty() {
	}

	public void qhelp() {
		System.out.println("rescue beauty");
	}

}

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="knight" class="com.knight.service.impl.BraveKnight">
	<constructor-arg ref="quest"></constructor-arg>
</bean>

<bean id="quest" class="com.quest.service.impl.RescueBeauty"></bean>

</beans>

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="knight" class="com.knight.service.impl.BraveKnight">
	<constructor-arg ref="quest"></constructor-arg>
</bean>

<bean id="quest" class="com.quest.service.impl.BitDragon"></bean>

</beans>

 

package com.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.knight.service.Knight;

public class KnightMain {

	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("rescueBeautyKnights.xml");
		Knight knight = (Knight) context.getBean("knight");
		knight.doAction();
		
		ApplicationContext context1 = new ClassPathXmlApplicationContext("bitDragonKnights.xml");
		Knight knight1 = (Knight) context1.getBean("knight");
		knight1.doAction();
	}
}

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SpringFirst</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    </welcome-file-list>

<context-param>
  <param-name>contextConfigLocation</param-name>
   <param-value>
   rescueBeautyKnights.xml,
   bitDragonKnights.xml
   
   </param-value>
  
</context-param>
   
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
    
</web-app>

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
spring-boot三层demo是一个基于spring-boot框架开发的三层架构应用的示例。 三层架构是一种软件设计模式,将应用程序分为表示层、业务逻辑层和数据访问层。这种架构能够提高代码的可读性、可维护性和可扩展性。 在这个demo中,表示层负责接收用户的请求,将请求转发给业务逻辑层。业务逻辑层处理业务逻辑,对请求进行处理,并调用数据访问层进行数据的读取或写入。数据访问层负责访问数据库或其他数据存储系统,提供对数据的增删改查等操作。 在spring-boot三层demo中,可以使用Spring MVC作为表示层,通过注解方式定义Controller,实现对请求的映射和处理。业务逻辑层可以使用@Service注解进行标记,使用依赖注入的方式获取数据访问层的实例,完成业务逻辑的处理。数据访问层可以使用Spring Data JPA等框架进行数据库操作,通过注解方式定义数据访问层的接口和实现类。 在这个demo中,可以实现一些简单的功能,比如用户管理。用户通过表示层的请求添加、删除、修改或查询用户信息。表示层将请求传递给业务逻辑层,业务逻辑层处理用户信息的校验、逻辑判断和业务操作,并调用数据访问层对数据库进行相应的增删改查。最后,数据访问层将操作结果返回给业务逻辑层,业务逻辑层将结果返回给表示层,表示层将结果展示给用户。 通过这个demo,可以帮助开发人员了解和掌握spring-boot三层架构的应用开发过程,提高应用程序的可维护性和可扩展性,并促进团队的协作开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值