postconstruct

Spring AOP and Post Construct

假装没事ソ 提交于 2019-12-06 00:14:26
I want to write the name of method which is using with @PostConstruct. But I found that AOP is unable to "Around" the PostConstruct method. Is there any way to use AOP with PostConstruct method? Give this a try. @Around("@annotation(javax.annotation.PostConstruct)") public void myAdvice(ProceedingJoinPoint jp) throws Throwable{ System.out.println("This is before " + jp.getSignature().getName() + "()"); jp.proceed(); } Additionally you need to activate compile-time weaving. I published a Demo project on github which uses maven. Clone https://github.com/jannikweichert/PostConstructAOPDemo and

@Singleton bean failed to initialize because of not expected transaction status 4 when it's marked TransactionAttribute=NOT_SUPPORTED

旧时模样 提交于 2019-12-05 19:17:52
I'm having difficulty with my EJB3.1 bean initialisation and more specifically to do with it failing due to perceived transaction rollback, even though I've marked the bean with @TransactionAttribute(NOT_SUPPORTED) . This should mean that any client transaction is paused on bean method entry until exit (when it will be resumed. It's definitely the transactional apprach I want. The "gist" of the code and error are as follows (note some of it is hand-cranked to hide details but is all relevant and representative): @Singleton(name = "MyClass") @ConcurrencyManagement(value = BEAN)

calling @PostConstruct on super bean AND extending bean

◇◆丶佛笑我妖孽 提交于 2019-12-05 03:44:15
I have a BaseBean with a @PostConstruct, and a bean extending it on which i would like to call another @PostConstruct. I have read several places where it said it was possible, however, it seems the @postConstruct on the extending class is called first (if the second is called at all). I then get a NPE on "context" because I'm assuming the super bean's PostConstruct has already been called. Is this do-able? If so what am I doing wrong? Base bean: @ManagedBean @RequestScoped public class BaseBean { @ManagedProperty(value = "#{contextBean}") private ContextBean contextBean; Context context;

How to test constructor of a class that has a @PostConstruct method using Spring?

穿精又带淫゛_ 提交于 2019-12-04 23:49:16
If I have a class with a @PostConstruct method, how can I test its constructor and thus its @PostConstruct method using JUnit and Spring? I can't simply use new ClassName(param, param) because then it's not using Spring -- the @PostConstruct method is not getting fired. Am I missing something obvious here? public class Connection { private String x1; private String x2; public Connection(String x1, String x2) { this.x1 = x1; this.x2 = x2; } @PostConstruct public void init() { x1 = "arf arf arf" } } @Test public void test() { Connection c = new Connection("dog", "ruff"); assertEquals("arf arf

Can @ManagedPropery and @PostConstruct be placed in a base class?

南楼画角 提交于 2019-12-04 08:49:58
I'm using a hierarchy of classes and what I would optimally try to do is have @ManagedBean 's that inherit a class that have @ManagedProperty members and @PostConstruct methods. Specifically, will this work? : public class A { @ManagedProperty private C c; @PostConstruct public void init() { // Do some initialization stuff } public C getC() { return c; } public void setC(C c) { this.c = c; } } @ManagedBean @SessionScoped public class B extends A { // Content... } Thanks in Advance! The @ManagedProperty is inherited and will just work that way. The @PostConstruct will also be inherited,

JSF injection with managed property, good pattern?

十年热恋 提交于 2019-12-02 04:36:13
问题 I'm quite new to JSF and not really "used" to the different thinking so I'm struggling on what (I assume) is basic. Lets say I have a class User, which is a session bean. Lets say I have a controller of 10000 objects, say Factory, that needs to be able to set some of them as "locked", in our case it means that the "locked" field does not become null anymore but reference a "LockedItem" object. This is where I can't get things working : LockedItem, when you instanciate it, is supposed to

PostConstruct is called twice

╄→尐↘猪︶ㄣ 提交于 2019-12-01 11:41:00
I use, JSF Spring OCPSoft Rewrite Glassfish 4 / Jetty 9 I've noticed that my beans invoke @PostConstruct 's init() method twice. Here's sample bean that got initialized twice, if you'll need web.xml or anything else, just post it - I ran out of ideas. @ManagedBean(name = "userBean") public class UserBean implements Serializable { private static final long serialVersionUID = -1347081883455053542L; @ManagedProperty(value = "#{param.username}") private String username; private Users user; private Authentication authentication; private StreamedContent avatar; @PostConstruct public void init() {

JSF PostConstruct Exception Handling - Redirect

核能气质少年 提交于 2019-12-01 08:00:46
I'd like to handle a JSF @PostConstruct exception by redirecting to another page. I'm using FacesContext.getCurrentInstance().getExternalContext().dispatch("page.jsf"); which works great but since the page uses 2 backing beans it continues to load the other backing bean (and if it encounters an error on the other backing bean it never gets to that dispatch/redirect). My question is.. is there a way to force that dispatch to happen right away and not load everything else? damian Look at this similar question: JSF navigation redirect to previous page According to BalusC you can use the following

JSF PostConstruct Exception Handling - Redirect

假装没事ソ 提交于 2019-12-01 05:51:54
问题 I'd like to handle a JSF @PostConstruct exception by redirecting to another page. I'm using FacesContext.getCurrentInstance().getExternalContext().dispatch("page.jsf"); which works great but since the page uses 2 backing beans it continues to load the other backing bean (and if it encounters an error on the other backing bean it never gets to that dispatch/redirect). My question is.. is there a way to force that dispatch to happen right away and not load everything else? 回答1: Look at this

Testing spring bean with post construct

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 14:03:09
问题 I have a bean similar to this: @Service public class A { @Autowired private B b; @PostConstruct public void setup() { b.call(param); } } @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = { Application.class, Config.class }) @WebIntegrationTest(randomPort = true) public class Test { @Autowired B b; @Before public void setUp() throws Exception { when(b.call(any())).thenReturn("smth"); } @Test public void test() throws Exception { // test... } } The problem is