我编写了一个JsonDeserializer包含自动服务的自定义服务,如下所示:
public class PersonDeserializer extends JsonDeserializer<Person> {
@Autowired
PersonService personService;
@Override
public Person deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
// deserialization occurs here which makes use of personService
return person;
}
}
Run Code Online (Sandbox Code Playgroud)
当我第一次使用这个解串器时,我得到了NPE,因为personService没有被自动装配.通过查看其他SO答案(特别是这一个),似乎有两种方法可以使自动装配工作.
选项1是SpringBeanAutowiringSupport在自定义反序列化器的构造函数中使用:
public PersonDeserializer() {
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
Run Code Online (Sandbox Code Playgroud)
选项2是使用a HandlerInstantiator并将其注册到我的ObjectMapperbean:
@Component
public class SpringBeanHandlerInstantiator extends HandlerInstantiator {
@Autowired
private ApplicationContext applicationContext;
@Override
public JsonDeserializer<?> deserializerInstance(DeserializationConfig config, Annotated annotated, Class<? extends JsonDeserializer<?>> deserClass) {
try {
return (JsonDeserializer<?>) applicationContext.getBean(deserClass); …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring查看JMS,并希望在我的MVC webapp启动时创建特定队列的一些并发使用者.
我在SO(/sf/answers/480280111/)的其他地方看到了以下XML配置:
<jms:listener-container concurrency="10">
<jms:listener destination="some.queue" ref="fooService" method="handleNewFoo"/>
</jms:listener-container>
Run Code Online (Sandbox Code Playgroud)
我在Java中使用Spring配置而不是XML.有人可以帮忙解决Spring注释的语法吗?
我现有的JmsConfiguration.java看起来像:
@Configuration
@ComponentScan(basePackages="net.domain.orders")
public class JmsConfiguration {
@Bean
public JmsTemplate jmsTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setDefaultDestination(new ActiveMQQueue("orders.queue"));
jmsTemplate.setConnectionFactory(connectionFactory());
return jmsTemplate;
}
@Bean
public ActiveMQConnectionFactory connectionFactory() {
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
activeMQConnectionFactory.setBrokerURL("tcp://localhost:61616");
return activeMQConnectionFactory;
}
}
Run Code Online (Sandbox Code Playgroud)
我搜索过很多例子,但到目前为止我发现的只是基于XML的.
我来自Perl背景,正在使用Spring编写我的第一个Java MVC Web应用程序.
我的webapp允许用户通过调用第三方SOAP服务来提交应用程序同步处理的订单.该项目的下一阶段是允许用户提交批量订单(例如包含500行的CSV)并异步处理它们.这是我现有控制器的片段:
@Controller
@Service
@RequestMapping(value = "/orders")
public class OrderController {
@Autowired
OrderService orderService;
@RequestMapping(value="/new", method = RequestMethod.POST)
public String processNewOrder(@ModelAttribute("order") Order order, Map<String, Object> map) {
OrderStatus orderStatus = orderService.processNewOrder(order);
map.put("orderStatus", orderStatus);
return "new";
}
}
Run Code Online (Sandbox Code Playgroud)
我计划创建一个新的@RequestMapping来处理传入的CSV并修改它OrderService以便能够将CSV分开并将各个订单持久保存到数据库中.
我的问题是:在MVC Spring应用程序中创建后台工作程序的最佳方法是什么?理想情况下,我将有5个线程处理这些订单,并且很可能来自队列.我已经读过@Async或提交过Runnable一个SimpleAsyncTaskExecutorbean,我不知道该走哪条路.一些例子对我有帮助.
我有一个包含另一个实体的实体,如下所示:
public class Order {
@Id
private int id;
@NotNull
private Date requestDate;
@NotNull
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="order_type_id")
private OrderType orderType;
}
public class OrderType {
@Id
private int id;
@NotNull
private String name;
}
Run Code Online (Sandbox Code Playgroud)
我有一个Spring MVC表单,用户可以在其中提交新订单; 他们必须填写的字段是请求日期并选择订单类型(这是一个下拉列表).
我正在使用Spring Validation来验证在尝试将orderType.id转换为OrderType时失败的表单输入.
我编写了一个自定义转换器来将orderType.id转换为OrderType对象:
public class OrderTypeConverter implements Converter<String, OrderType> {
@Autowired
OrderTypeService orderTypeService;
public OrderType convert(String orderTypeId) {
return orderTypeService.getOrderType(orderTypeId);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我不知道如何使用java配置使用Spring注册此转换器.我发现的XML等价物(来自Spring MVC中的Dropdown值绑定)是:
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="OrderTypeConverter"/>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
从搜索网络我似乎找不到相当于java配置 - 有人可以帮助我吗?
UPDATE
我已将OrderTypeConvertor添加到WebMvcConfigurerAdapter,如下所示: …
我使用for_each循环创建了多个子网和多个 VPC 终端节点,如下所示:
### VARIABLES ###
variable "private_cidr_mask" {
default = {
"us-west-1a" = "10.0.1.0/24"
"us-west-1b" = "10.0.2.0/24"
}
}
variable "vpc_endpoints" {
default = [
"com.amazonaws.us-west-1.ecs-agent",
"com.amazonaws.us-west-1.ecs-telemetry",
"com.amazonaws.us-west-1.ecs"
]
}
### RESOURCES ###
resource "aws_subnet" "private_subnet" {
for_each = var.private_cidr_mask
vpc_id = aws_vpc.vpc.id
availability_zone = each.key
cidr_block = each.value
}
resource "aws_vpc_endpoint" "vpc_endpoint" {
for_each = toset(var.vpc_endpoints)
vpc_id = aws_vpc.vpc.id
vpc_endpoint_type = "Interface"
service_name = each.value
security_group_ids = [ aws_security_group.security_group.id ]
private_dns_enabled = true
}
Run Code Online (Sandbox Code Playgroud)
现在,我必须使用以下命令将每个 VPC …
我正在Spring Boot中开发一个多模块项目,其项目结构如下:
com.app.parent <- parent pom with version numbers and common dependencies (POM)
com.app.core <- repository and service layer, models, DTOs (JAR)
com.app.rest <- rest API (WAR)
com.app.soap <- soap API (WAR)
Run Code Online (Sandbox Code Playgroud)
父项目的pom.xml文件是:
<artifactId>app-parent</artifactId>
<packaging>pom</packaging>
<name>app-parent</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
核心项目的pom.xml文件是:
<artifactId>app-core</artifactId>
<packaging>jar</packaging>
<name>app-core</name>
<parent>
<groupId>com.app</groupId>
<artifactId>app-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../app-parent/pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>mysql</groupId> …Run Code Online (Sandbox Code Playgroud) 当我更新与 a 关联的 AMI 时aws_launch_template,Terraform 会按预期创建启动模板的新版本,并且还将 更新aws_autoscaling_group为指向启动模板的新版本。
但是,没有执行“滚动更新”来使用基于新 AMI 的新实例切换现有实例,我必须手动终止现有实例,然后 ASG 使用新 AMI 启动新实例。
我必须对配置进行哪些更改才能让 Terraform 执行滚动更新?
现有代码如下:
resource "aws_launch_template" "this" {
name_prefix = "my-launch-template-"
image_id = var.ami_id
instance_type = "t3.small"
key_name = "testing"
vpc_security_group_ids = [ aws_security_group.this.id ]
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "this" {
name_prefix = "my-asg-"
vpc_zone_identifier = var.subnet_ids
target_group_arns = var.target_group_arns
health_check_type = "ELB"
health_check_grace_period = 300
default_cooldown = 10
min_size = 4
max_size = 4
desired_capacity = …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 AMI 从一个 AWS 账户复制到另一个账户,并使用目标账户中的 CMK 对其进行加密。
CMK 上的关键策略是:
{
"Version": "2012-10-17",
"Id": "key-default",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::TARGET-ACCOUNT-NUMBER:root"
},
"Action": "kms:*",
"Resource": "*"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我在目标账户中创建了一个具有以下策略的角色:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"kms:ListAliases",
"kms:GenerateDataKey",
"kms:DescribeKey",
"kms:Encrypt",
"ec2:CopyImage"
],
"Resource": "*"
}
]
}
Run Code Online (Sandbox Code Playgroud)
与此角色相关的还有AmazonEC2ReadOnlyAccess策略。
如果我登录到 root 账户并承担目标账户中的角色,然后尝试使用我的 CMK 复制 AMI,它会失败并显示Snapshot snap-abc123xyz is in an unexpected state: error. 快照上没有其他信息来指示根本原因。
如果我将AdministratorAccess策略附加到 AMI 复制的角色,那么它一定是权限问题。
有人可以提供复制带有加密的 AMI 所需的权限列表吗?
我使用 tomcat:8.5-jre8-alpine 映像在 ECS 中部署了一个 Java webapp。此任务的网络模式为 awsvpc;我有许多这样的任务在由 ALB 前置的 3 个 EC2 实例上运行。
这工作正常,但现在我想在每个 tomcat 容器前面添加一个 nginx 反向代理,类似于这个例子:https : //github.com/awslabs/ecs-nginx-reverse-proxy/tree/master/reverse -代理。
我的缩写容器定义文件是:
{
"containerDefinitions": [
{
"name": "nginx",
"image": "<NGINX reverse proxy image URL>",
"memory": "256",
"cpu": "256",
"essential": true,
"portMappings": [
{
"containerPort": "80",
"protocol": "tcp"
}
],
"links": [
"app"
]
},
{
"name": "app",
"image": "<app image URL>",
"memory": "1024",
"cpu": "1024",
"essential": true
}
],
"volumes": [],
"networkMode": "awsvpc",
"placementConstraints": [],
"family": "application-stack" …Run Code Online (Sandbox Code Playgroud) 我正在开始使用 Hibernate,有一个关于 InheritanceType.JOINED 和 @PrimaryKeyJoinColumn 的问题。
给定以下数据库表,其中员工引用人员,经理引用员工:
create table person (person_id int(10) auto_increment,
name varchar(100),
primary key (person_id));
create table employee (employee_id int(10) auto_increment,
person_id int(10),
salary int(10),
primary key (employee_id));
create table manager (manager_id int(10) auto_increment,
employee_id int(10),
shares int(10),
primary key (manager_id));
Run Code Online (Sandbox Code Playgroud)
我可以为 Person 和 Employee 创建前两个类,如下所示:
@Entity
@Table(name="person")
@Inheritance(strategy=InheritanceType.JOINED)
public class Person {
@Id
@GeneratedValue
@Column(name="person_id")
private int personId;
@Column(name="name")
private String name;
}
@Entity
@Table(name="employee")
@PrimaryKeyJoinColumn(name="person_id")
public class Employee extends Person {
@GeneratedValue
@Column(name="employee_id")
private int …Run Code Online (Sandbox Code Playgroud) 我正在使用 hibernate-validator 5.1.2.Final 和 Spring 4.0.6.RELEASE 并希望利用快速失败功能,以便当我配置具有多个约束的注释时,它们不会全部执行,而只会执行第一个错误消息被返回。
@MultipartFileNotEmpty
@CsvFile
@Documented
@Constraint(validatedBy = { })
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OrderCsv {
String message() default "";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Run Code Online (Sandbox Code Playgroud)
通过查看Hibernate Validator文档,我似乎可以在全局级别启用fail_fast,但我不确定如何在 Java 配置中启用它。我当前的休眠配置如下所示:
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages="uk.co.project")
public class HibernateConfig {
final static String JDBC_URL = "jdbc:mysql://localhost:3306/";
final static String DRIVER_CLASS = "com.mysql.jdbc.Driver";
@Autowired
private Environment environment;
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new org.apache.commons.dbcp2.BasicDataSource();
dataSource.setDriverClassName(DRIVER_CLASS);
dataSource.setUsername(environment.getProperty("datasource.username"));
dataSource.setPassword(environment.getProperty("datasource.password"));
dataSource.setUrl(JDBC_URL + environment.getProperty("datasource.database")); …Run Code Online (Sandbox Code Playgroud) 我有@Repository如下内容:
@Repository
public class OrderRepository {
@Autowired
SessionFactory sessionFactory;
public void update(Order order) {
sessionFactory.getCurrentSession().update(order);
}
}
Run Code Online (Sandbox Code Playgroud)
由a调用@Service如下:
@Transactional
@Service
public class OrderService {
@Autowired
OrderRepository orderRepository;
public void updateOrder(Order order) {
orderRepository.update(order);
}
}
Run Code Online (Sandbox Code Playgroud)
在某些情况下,正在更新的订单已从数据库中删除(这是预期的并且是良性的)。发生这种情况时,将引发异常:
SEVERE: Servlet.service() for servlet [DispatcherServlet] in context with path [/project] threw exception [Request processing failed; nested exception is org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update …Run Code Online (Sandbox Code Playgroud) java ×8
spring ×5
hibernate ×3
spring-mvc ×3
terraform ×2
amazon-ec2 ×1
amazon-ecs ×1
amazon-iam ×1
aws-kms ×1
docker ×1
ec2-ami ×1
exception ×1
jackson ×1
jpa ×1
spring-boot ×1
validation ×1