当前位置: 首页 > wzjs >正文

闲鱼做网站靠谱吗百度识图在线入口

闲鱼做网站靠谱吗,百度识图在线入口,建站宝盒自助建站系统,日本韩国出线了吗以下是一个 Spring XML 注解的混合配置示例,结合了 XML 的基础设施配置(如数据源、事务管理器)和注解的便捷性(如依赖注入、事务声明)。所有业务层代码通过注解简化,但核心配置仍通过 XML 管理。 1. 项目结…

以下是一个 Spring XML + 注解的混合配置示例,结合了 XML 的基础设施配置(如数据源、事务管理器)和注解的便捷性(如依赖注入、事务声明)。所有业务层代码通过注解简化,但核心配置仍通过 XML 管理。


1. 项目结构

src/main/java
├── com.example.dao
│   └── UserDao.java           # DAO 接口
│   └── impl
│       └── UserDaoImpl.java   # DAO 实现类(注解驱动)
├── com.example.service
│   ├── UserService.java       # Service 接口
│   └── impl
│       └── UserServiceImpl.java # Service 实现类(@Service + @Transactional)
└── com.example.controller└── UserController.java    # Controller 类(@Controller)
resources
├── applicationContext.xml     # Spring 主配置文件
└── db.properties              # 数据库配置文件

2. XML 配置 (applicationContext.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 1. 启用组件扫描(自动发现 @Component, @Service, @Repository, @Controller) --><context:component-scan base-package="com.example"/><!-- 2. 数据源配置(通过 properties 文件注入) --><context:property-placeholder location="classpath:db.properties"/><bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"><property name="jdbcUrl" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!-- 3. JdbcTemplate 配置(用于 DAO 层) --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/></bean><!-- 4. 事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!-- 5. 启用注解驱动的事务管理(@Transactional) --><tx:annotation-driven transaction-manager="transactionManager"/></beans>

3. DAO 层(注解驱动)

接口 (UserDao.java)
package com.example.dao;import com.example.model.User;
import org.springframework.stereotype.Repository;@Repository // 标记为 DAO 组件,自动被组件扫描发现
public interface UserDao {User findById(int id);
}
实现类 (UserDaoImpl.java)
package com.example.dao.impl;import com.example.dao.UserDao;
import com.example.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;@Repository // 替代 XML 中的 <bean> 定义
public class UserDaoImpl implements UserDao {@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic User findById(int id) {String sql = "SELECT * FROM users WHERE id = ?";return jdbcTemplate.queryForObject(sql, new Object[]{id}, (rs, rowNum) ->new User(rs.getInt("id"), rs.getString("name")));}
}

4. Service 层(注解驱动 + 事务)

接口 (UserService.java)
package com.example.service;import com.example.model.User;public interface UserService {User getUserById(int id);
}
实现类 (UserServiceImpl.java)
package com.example.service.impl;import com.example.dao.UserDao;
import com.example.model.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; // 声明事务@Service // 替代 XML 中的 <bean> 定义
@Transactional // 默认事务配置(REQUIRED 传播行为)
public class UserServiceImpl implements UserService {@Autowiredprivate UserDao userDao;@Overridepublic User getUserById(int id) {return userDao.findById(id);}
}

5. Controller 层(注解驱动)

类 (UserController.java)
package com.example.controller;import com.example.model.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController // 替代 XML 中的 <bean> 定义
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/users/{id}")public User getUser(@PathVariable int id) {return userService.getUserById(id);}
}

6. 模型类 (User.java)

package com.example.model;public class User {private int id;private String name;public User(int id, String name) {this.id = id;this.name = name;}// Getter 和 Setter 省略
}

7. 数据库配置 (db.properties)

jdbc.url=jdbc:mysql://localhost:3306/testdb
jdbc.username=root
jdbc.password=secret

8. 启动应用

主类 (Application.java)
package com.example;import com.example.controller.UserController;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Application {public static void main(String[] args) {// 初始化 Spring 上下文(仅加载 XML 配置)ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");// 通过组件扫描自动发现 UserControllerUserController userController = context.getBean(UserController.class);User user = userController.getUser(1);System.out.println("User: " + user.getName());// 关闭上下文context.close();}
}

关键点说明

  1. 混合配置的优势

    • XML:管理数据源、事务管理器等基础设施。
    • 注解:简化业务层的依赖注入(@Autowired)和事务声明(@Transactional)。
  2. 组件扫描

    • <context:component-scan> 自动发现 @Component@Service@Repository@Controller 注解的类,无需 XML 定义 Bean。
  3. 事务管理

    • <tx:annotation-driven> 启用 @Transactional 注解,替代 XML 中的 AOP 事务配置。
  4. 依赖注入

    • 所有依赖通过 @Autowired 注入,无需 XML 中的 <property> 标签。

常见问题排查

  1. Bean 未找到

    • 检查 <context:component-scan> 的包路径是否包含所有组件。
    • 确保类上有正确的注解(如 @Service@Repository)。
  2. 事务不生效

    • 检查 @Transactional 是否添加到 public 方法。
    • 确保 <tx:annotation-driven> 已配置且事务管理器正确。
  3. 数据库连接失败

    • 检查 db.properties 中的 URL、用户名和密码。
    • 确保数据库驱动已添加到依赖(如 MySQL 的 mysql-connector-java)。

通过这种混合模式,既能享受 XML 的集中式基础设施配置,又能利用注解简化业务层代码,是传统 Spring 项目的推荐实践。

http://www.dtcms.com/wzjs/340951.html

相关文章:

  • 哪个网站能下载gif正规seo多少钱
  • 政府门户网站建设现状百度统计app
  • 搜狗提交网站收录入口制作网站的平台
  • 论述网站建设及运营流程东莞网站建设公司
  • 网易云播放器做网站播放seo 网站排名
  • 网站建设 项目书 框架semseo
  • 做网站域名要自己注册吗广州营销型网站
  • 怎么申请一个商城网站.网络推广网站
  • 贵港哪里有网站建设推广优化推广seo
  • 织梦网站怎么做投票百度地址
  • 专业型网站建设方案网站如何快速被百度收录
  • 做网站相关人员黑帽seo工具
  • 网站地图做计划任务上海网络优化服务
  • 自媒体还是做网站企业网站快速排名
  • 官方做任务网站百度统计
  • 建设工程类的网站企业宣传册模板
  • 公司做网站需要什么资料合肥网站关键词排名
  • 装饰行业网站建设最快新闻资讯在哪看
  • 小型网站开发要多少钱手机网站建设价格
  • java程序员自己做网站seo网络推广经理
  • 免费微信快速开发平台网址seo关键词
  • 沈阳市城市建设网站seo优化要做什么
  • 株洲网站开发公司微信公众号怎么推广
  • 如何让各大搜索引擎识别新建网站网站发布流程
  • 网站备案注意海外推广
  • 年度网站信息化建设工作计划湖南seo网站开发
  • 在国外做网站营销推广运营
  • 用vs2010做网站武汉seo关键字优化
  • 做网站要服务器和什么宁波seo网络推广软件系统
  • 成都广告印刷公司seo研究中心教程