@GetMapping和之间有什么区别@RequestMapping(method = RequestMethod.GET)?
我在一些Spring Reactive示例中看到过,
@GetMapping而不是使用它@RequestMapping
我决定学习一些 Java 的主要文档。我已经开始Object上课,我正在研究equals方法。我知道这equals是使用hashCode方法。虽然,hashCode返回 int 因此可以生成的唯一哈希码数量有限。
当我尝试比较两个相同的不同对象时会发生什么hashCode?这甚至可能吗?
我已经开始使用Spring Data JPA了.不幸的是我无法配置它.我有Entity类,Repository接口,但是当我尝试测试它时,出了点问题.
源代码(我有setter/getter方法,但我跳过它以获得更好的代码):
@Entity
public class Employee {
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
public Employee() {
}
}
Run Code Online (Sandbox Code Playgroud)
存储库类:
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}
Run Code Online (Sandbox Code Playgroud)
组态:
@Configuration
@ComponentScan(basePackages = "com.szymon.repository")
@EnableJpaRepositories(basePackages = "com.szymon.repository")
public class AppConfig {
@Bean
public LocalEntityManagerFactoryBean entityManagerFactoryBean() {
LocalEntityManagerFactoryBean localEntityManagerFactoryBean = new LocalEntityManagerFactoryBean();
localEntityManagerFactoryBean.setPersistenceUnitName("postgres");
return localEntityManagerFactoryBean;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试测试它时:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = EmployeeRepository.class)
public class EmployeeRepositoryTest {
@Autowired
private EmployeeRepository employeeRepository;
@Test
public …Run Code Online (Sandbox Code Playgroud) @Autowired当没有定义应该自动装配的 bean 时,默认的Spring 实现会抛出错误。是否可以配置 Spring,将 null 分配给对象而不是抛出异常?
编辑:
我已经添加required=false,Autowired但它仍然无法正常工作。那是我的代码:
@Autowired
private ApplicationContext applicationContext;
@Autowired(required = false)
private HelloService helloService;
public HelloController() {
message = "Hello World";
System.out.println("Controller constructor");
}
@RequestMapping(method = RequestMethod.GET)
public ModelAndView helloWorld() {
ModelAndView modelAndView = new ModelAndView("hello");
if (helloService == null) {
System.out.println(message);
} else {
helloService.hello();
BeanDefinitionRegistry factory = (BeanDefinitionRegistry) applicationContext.getAutowireCapableBeanFactory();
factory.removeBeanDefinition("helloService");
}
return modelAndView;
}
Run Code Online (Sandbox Code Playgroud)
在第一个请求中它是自动装配的,但是在删除 bean 后的下一个请求中factory.removeBeanDefinition("helloService"),再次构造控制器 bean,我得到NoSuchBeanDefinitionException
编辑2:
我创建了另一个具有以下主体的控制器:
@Autowired(required = false)
private TestService …Run Code Online (Sandbox Code Playgroud) 我将为我的应用程序创建自定义的流畅API.我决定检查现有库的代码,并在至少2个使用流畅api的项目中发现了一些怪物.
我看到有很多类和接口,只有通用类型的数量不同.
例如:在jOOQ库DerivedColumnList22中
在RxJava中:Action9
像这样的怪物的目的是什么?仅适用于性能案例吗?或者是流行api的一些共性和方式?
当你看到像这样的怪物时,用流畅的api实现自己的DSL看起来很可怕.
我正在尝试实现我自己的 BeanPostProcessor 实现。
@Component
public class UserDetailsProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof User) {
User user = (User) bean;
List<Annotation> beanAnnotations = Arrays.asList(user.getClass().getAnnotations());
for (Annotation annotation : beanAnnotations) {
UserDetails userDetails = (UserDetails) annotation;
System.out.println(userDetails.firstName());
System.out.println(userDetails.lastName());
}
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 bean 定义:
@SpringBootApplication
public class HelloReactiveApplication {
public static void main(String[] args) { …Run Code Online (Sandbox Code Playgroud) 在 Spring MVC 中,当请求到来时,DispatcherServlet指定HandlerMapping指示哪个控制器应该处理请求。Controller处理请求并返回指定的模型View。
我不明白在那之后会发生什么,什么时候ViewResolver开始他的工作。如果View已经由 指定Controller,为什么还有一个ViewResolver层?它实际上有什么作用?不应该View在Controller?之前指定或与之平行?或者Controller应该触发ViewResolver层?
我正在尝试创建一个正方形网格以提供一些导航功能。
我有它与js一起使用,但我不喜欢这种解决方案。我正在使用Bootstrap 3
<div class="container">
<div class="row">
<div class="col-xs-4" style="background-color: lightgray"></div>
<div class="col-xs-4" style="background-color: lightgreen"></div>
<div class="col-xs-4" style="background-color: lightgray"></div>
<div class="col-xs-4" style="background-color: lightgreen"></div>
<div class="col-xs-4" style="background-color: lightgray"></div>
<div class="col-xs-4" style="background-color: lightgreen"></div>
</div>
</div>
var divs = $(".row > div");
var width = divs.width();
divs.height(width)
Run Code Online (Sandbox Code Playgroud)
如何仅使用CSS实现它?
我想为插入操作创建触发器,以及打印插入值的过程。
CREATE TRIGGER added_product_info_trigger
BEFORE INSERT
ON products
EXECUTE PROCEDURE added_product_info();
Run Code Online (Sandbox Code Playgroud)
还有我的程序
CREATE OR REPLACE FUNCTION added_product_info()
RETURNS trigger
AS
$$
(Select p.productname, s.companyname from products as p, suppliers as s
where p.supplierid = s.supplierid)
$$ LANGUAGE SQL;
Run Code Online (Sandbox Code Playgroud)
如何打印我插入的值?
java ×8
spring ×6
spring-mvc ×2
annotations ×1
collision ×1
css ×1
dsl ×1
fluent ×1
hibernate ×1
html ×1
javascript ×1
jpa ×1
jquery ×1
object ×1
plpgsql ×1
postgresql ×1
solr ×1
spring-4 ×1
spring-boot ×1
sql ×1
triggers ×1
web ×1