springboot用法和与各种框架、组件等结合使用,包括springboot的注解分析、路径分析、取得内置对象、项目打包、配置环境属性、读取资源文件、Bean配置、模板渲染、基于yml的多profile配置、基于properties的多profile、添加数据验证、配置错误页、全局异常处理、配置Tomcat运行、配置https访问路径、信息显示、内置对象、显示对象信息、文件上传、使用文件服务器、拦截器、配置druid数据库连接池、整合MyBatis框架、事务控制、Druid监控、整合ActiveMQ、整合RabbitMQ、整合kafka、邮件发送、定时调度、Actuator监控、整合Redis数据库、整合Restfui框架、整合Shiro
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package cn.mldn.microboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@SpringBootApplication // 启动SpringBoot程序,而后自带子包扫描
|
||||
@EnableTransactionManagement
|
||||
public class StartSpringBootMain {
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(StartSpringBootMain.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package cn.mldn.microboot.config;
|
||||
|
||||
import javax.jms.Queue;
|
||||
|
||||
import org.apache.activemq.command.ActiveMQQueue;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jms.annotation.EnableJms;
|
||||
|
||||
@Configuration
|
||||
@EnableJms
|
||||
public class ActiveMQConfig {
|
||||
@Bean
|
||||
public Queue queue() {
|
||||
return new ActiveMQQueue("mldn.msg.queue") ;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package cn.mldn.microboot.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.support.http.StatViewServlet;
|
||||
import com.alibaba.druid.support.http.WebStatFilter;
|
||||
|
||||
@Configuration
|
||||
public class DruidConfig {
|
||||
@Bean
|
||||
public ServletRegistrationBean druidServlet() { // 主要实现WEB监控的配置处理
|
||||
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
|
||||
new StatViewServlet(), "/druid/*"); // 现在要进行druid监控的配置处理操作
|
||||
servletRegistrationBean.addInitParameter("allow",
|
||||
"127.0.0.1,192.168.28.159"); // 白名单
|
||||
servletRegistrationBean.addInitParameter("deny", "192.168.28.200"); // 黑名单
|
||||
servletRegistrationBean.addInitParameter("loginUsername", "mldnjava"); // 用户名
|
||||
servletRegistrationBean.addInitParameter("loginPassword", "hello"); // 密码
|
||||
servletRegistrationBean.addInitParameter("resetEnable", "false"); // 是否可以重置数据源
|
||||
return servletRegistrationBean ;
|
||||
}
|
||||
@Bean
|
||||
public FilterRegistrationBean filterRegistrationBean() {
|
||||
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean() ;
|
||||
filterRegistrationBean.setFilter(new WebStatFilter());
|
||||
filterRegistrationBean.addUrlPatterns("/*"); // 所有请求进行监控处理
|
||||
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*");
|
||||
return filterRegistrationBean ;
|
||||
}
|
||||
@Bean
|
||||
@ConfigurationProperties(prefix = "spring.datasource")
|
||||
public DataSource druidDataSource() {
|
||||
return new DruidDataSource();
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package cn.mldn.microboot.consumer;
|
||||
|
||||
import org.springframework.jms.annotation.JmsListener;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MessageConsumerService {
|
||||
@JmsListener(destination="mldn.msg.queue")
|
||||
public void receiveMessage(String text) { // 进行消息接收处理
|
||||
System.err.println("【*** 接收消息 ***】" + text);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package cn.mldn.microboot.controller;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import cn.mldn.microboot.service.IDeptService;
|
||||
import cn.mldn.microboot.util.controller.AbstractBaseController;
|
||||
|
||||
@RestController
|
||||
public class DeptController extends AbstractBaseController {
|
||||
@Resource
|
||||
private IDeptService deptService ;
|
||||
@RequestMapping(value = "/list", method = RequestMethod.GET)
|
||||
public Object list() { // 通过model可以实现内容的传递
|
||||
return this.deptService.list() ;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.mldn.microboot.dao;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
import cn.mldn.microboot.vo.Dept;
|
||||
@Mapper
|
||||
public interface IDeptDAO {
|
||||
public List<Dept> findAll();
|
||||
public boolean doCreate(Dept vo) ;
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package cn.mldn.microboot.producer;
|
||||
|
||||
public interface IMessageProducerService {
|
||||
public void sendMessage(String msg) ;
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package cn.mldn.microboot.producer.impl;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.jms.Queue;
|
||||
|
||||
import org.springframework.jms.core.JmsMessagingTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import cn.mldn.microboot.producer.IMessageProducerService;
|
||||
@Service
|
||||
public class MessageProducerServiceImpl implements IMessageProducerService {
|
||||
@Resource
|
||||
private JmsMessagingTemplate jmsMessagingTemplate;
|
||||
@Resource
|
||||
private Queue queue;
|
||||
@Override
|
||||
public void sendMessage(String msg) {
|
||||
this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.mldn.microboot.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import cn.mldn.microboot.vo.Dept;
|
||||
|
||||
public interface IDeptService {
|
||||
@Transactional(readOnly = true)
|
||||
public List<Dept> list() ;
|
||||
@Transactional(propagation=Propagation.REQUIRED)
|
||||
public boolean add(Dept vo) ;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cn.mldn.microboot.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import cn.mldn.microboot.dao.IDeptDAO;
|
||||
import cn.mldn.microboot.service.IDeptService;
|
||||
import cn.mldn.microboot.vo.Dept;
|
||||
import cn.mldn.microboot.vo.Member;
|
||||
@Service
|
||||
public class DeptServiceImpl implements IDeptService {
|
||||
@Resource
|
||||
private IDeptDAO deptDAO;
|
||||
@Override
|
||||
public List<Dept> list() {
|
||||
return this.deptDAO.findAll();
|
||||
}
|
||||
@Override
|
||||
public boolean add(Dept vo) {
|
||||
return this.deptDAO.doCreate(vo);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package cn.mldn.microboot.util.controller;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.springframework.context.MessageSource;
|
||||
|
||||
public abstract class AbstractBaseController {
|
||||
@Resource
|
||||
private MessageSource messageSource; // 自动注入此资源对象
|
||||
public String getMessage(String key, String... args) {
|
||||
return this.messageSource.getMessage(key, args, Locale.getDefault());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package cn.mldn.microboot.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class Dept implements Serializable {
|
||||
private Long deptno ;
|
||||
private String dname ;
|
||||
public Long getDeptno() {
|
||||
return deptno;
|
||||
}
|
||||
public void setDeptno(Long deptno) {
|
||||
this.deptno = deptno;
|
||||
}
|
||||
public String getDname() {
|
||||
return dname;
|
||||
}
|
||||
public void setDname(String dname) {
|
||||
this.dname = dname;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Dept [deptno=" + deptno + ", dname=" + dname + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package cn.mldn.microboot.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class Member implements Serializable {
|
||||
private Long mid ;
|
||||
private String name ;
|
||||
private Integer age ;
|
||||
private Date birthday ;
|
||||
private Double salary ;
|
||||
public Long getMid() {
|
||||
return mid;
|
||||
}
|
||||
public void setMid(Long mid) {
|
||||
this.mid = mid;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
public Date getBirthday() {
|
||||
return birthday;
|
||||
}
|
||||
public void setBirthday(Date birthday) {
|
||||
this.birthday = birthday;
|
||||
}
|
||||
public Double getSalary() {
|
||||
return salary;
|
||||
}
|
||||
public void setSalary(Double salary) {
|
||||
this.salary = salary;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user