Java EE 7에서는 @Named 주석의 목적과 용도는 무엇입니까? 그것 없이도 컨테이너는 런타임에이 빈을 발견 할 수 있어야합니다.@Named annotation의 목적은 무엇입니까?
또한 @Singleton은 어떤 역할을합니까? 개발자가 애플리케이션에서 다중 인스턴스를 생성 할 필요가 없다면 여기에서 싱글 톤을 사용할 필요가 없습니다.
1) 내가 @Singleton을 제거하면 나는 1로 incrementA() 또는 incrementB(), neighter 단위를 클릭 할 때, 값은 1
에서 숙박 :@Singleton
@Named
public class Counter {
private int a = 1;
private int b = 1;
public void incrementA() {
a++;
}
public void incrementB() {
b++;
}
public int getA() {
return a;
}
public int getB() {
return b;
}
}
나는 두 가지 테스트를했다
2) @Named 주석을 제거하면 null 포인터 예외가보고됩니다.
Java EE를 배우고 있지만이 동작을 잘 이해하지 못했습니다. (빈 사용 방법)
편집 :
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<body>
<ui:composition template="/template.xhtml">
<ui:define name="title">
Helloworld EJB 3.1 Singleton Quickstart
</ui:define>
<ui:define name="body">
<p>
This example demonstrates a singleton session bean that maintains state for 2 variables: <code>a</code> and <code>b</code>.
</p>
<p>
A counter is incremented when you click on the
link to the variable name. If you close and restart your browser, or
if you have multiple browsers, you can see that the counter always
increments the last value. These values are maintained until you
restart the application. To test the singleton bean, click on either
"Increment" button below.
</p>
<table>
<tr>
<h:form>
<td><b>Counter A</b></td><td><h:commandButton value="Increment" action="#{counter.incrementA}" /></td><td>#{counter.a}</td>
</h:form>
</tr>
<tr>
<h:form>
<td><b>Counter B</b></td><td><h:commandButton value="Increment" action="#{counter.incrementB}" /></td><td>#{counter.b}</td>
</h:form>
</tr>
</table>
</ui:define>
</ui:composition>
~
일단 @Singleton이 제거되면 카운터가 작동하지 않는 정확한 이유는 무엇입니까? 카운터의 값은 1로 유지되고 증가하지 않습니다. – marlon
값이 동일하게 유지되지만 웹 응용 프로그램에 액세스 할 때마다 새 인스턴스를 얻는 것은 아니라고 생각합니다. 불행히도 나는 어떤 상황에서 클래스가 사용되는지를 볼 수 없다. –
어떻게 사용되는지 확인하십시오. 아주 간단합니다. 이것은 싱글 톤 세션 빈의 개념을 보여주는 간단한 데모입니다. "Increment"를 클릭 할 때마다 새로운 인스턴스가 생깁니 까? 하지만 기본 값은 1입니다. 단 한 번 클릭해도 새 인스턴스의 값은 2가되어야합니까? – marlon